Friday, August 9, 2013

Hibernate - JNDI Configuration

First some points about JNDI:

  1. It maps an object with a name.
  2. It requires an Application Server.

Scenarios:

  1. Hibernate can use the datasource created by JNDI for making connection with the database. This makes Hibernate free of knowledge about the database connection.
  2. Hibernate can manage the connection with the database and use JNDI for mapping the           SessionFactory object, so that the SessionFactory can be accessed from any part of the application.
  3. Combination of the above two. Hibernate can use the datasource created by JNDI for making     connection with the database and map the SessionFactory object.


HOW TO


Here I used Eclipse Kepler, JBoss Application Server 7.1.1, MySQL.

Scenario 1:

  • Create folder under JBoss AS directory with following address modules\com\mysql\main. Goto the main directory.
  • Copy and paste the database-java connector(JDBC) jar file. I used mysql-connector-java-5.1.18-bin.jar
  • Create module.xml with following contents.
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.18-bin.jar" />
</resources>
<dependencies>
<module name="javax.api" />
<module name="javax.transaction.api" />
<module name="javax.servlet.api" optional="true" />
</dependencies>
</module>
view raw blog_module.xml hosted with ❤ by GitHub

  • Open standalone.xml file under standalone\configuration in JBoss AS directory.
  • Add the following under <drivers> in <datasources> element.
<driver name="mysql" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
</driver>
view raw blog_drivers hosted with ❤ by GitHub
  • Add the following under <datasources> element.
<datasource jndi-name="java:/comp/env/jdbc/hibernate/MySQLDB" pool-name="MySQLDB" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/DBNAME</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql</driver>
<security>
<user-name>USERNAME</user-name>
<password>PASSWORD</password>
</security>
</datasource>
view raw blog_datasource hosted with ❤ by GitHub
  • Create or Edit web.xml of your project so that it has following content.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Hibernate Example</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-env-ref>
<resource-env-ref-name>java:/comp/env/jdbc/hibernate/MySQLDB</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
</web-app>
view raw blog_web.xml hosted with ❤ by GitHub
  • Edit hibernate.cfg.xml.
<property name="hibernate.connection.datasource">java:/comp/env/jdbc/hibernate/MySQLDB</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="current_session_context_class">thread</property>
  • To access the connection in your application.
import javax.naming.InitialContext;
import javax.sql.DataSource;
DataSource ds = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/hibernate/MySQLDB");
ds.getConnection().createStatement().executeUpdate("INSERT INTO …");

Scenario 2:

  • In hibernate.cfg.xml file change <session-factory> to <session-factory name="SessionFactory">
  • Have this in a start up code.
new Configuration().configure().buildSessionFactory();
  • To access the SessionFactory,
import javax.naming.InitialContext;
(SessionFactory) new InitialContext().lookup("SessionFactory");


Sample Code:

import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory = generateSessionFactory();
private static SessionFactory generateSessionFactory(){
new Configuration().configure().buildSessionFactory();
try {
return (SessionFactory) new InitialContext().lookup("SessionFactory");
} catch (NamingException e) {
e.printStackTrace();
throw new IllegalStateException("SessionFactory could not be generated.");
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}



Scenario 3:

Just the Combination of the above two scenarios.



No comments:

Post a Comment