First some points about JNDI:
- It maps an object with a name.
- It requires an Application Server.
Scenarios:
- Hibernate can use the datasource created by JNDI for making connection with the database. This makes Hibernate free of knowledge about the database connection.
- 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.
- 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
- Open standalone.xml file under standalone\configuration in JBoss AS directory.
- Add the following under <drivers> in <datasources> element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<driver name="mysql" module="com.mysql"> | |
<xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class> | |
</driver> |
- Add the following under <datasources> element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
- Create or Edit web.xml of your project so that it has following content.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
- Edit hibernate.cfg.xml.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new Configuration().configure().buildSessionFactory(); |
- To access the SessionFactory,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.naming.InitialContext; | |
(SessionFactory) new InitialContext().lookup("SessionFactory"); |
Sample Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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