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.
- Open standalone.xml file under standalone\configuration in JBoss AS directory.
- Add the following under <drivers> in <datasources> element.
- Add the following under <datasources> element.
- Create or Edit web.xml of your project so that it has following content.
- Edit hibernate.cfg.xml.
- To access the connection in your application.
Scenario 2:
- In hibernate.cfg.xml file change <session-factory> to <session-factory name="SessionFactory">
- Have this in a start up code.
- To access the SessionFactory,
Sample Code:
Scenario 3:
Just the Combination of the above two scenarios.
No comments:
Post a Comment