Share on Google+Share on Google+

hibernate jndi

Advertisement
In this section, you will learn about configuring hibernate to connect to database via JNDI datasource.

hibernate jndi

In this section, you will learn about configuring hibernate to connect to database via JNDI datasource.

You need to do following thing to configure hibernate for connecting database via JNDI datasource :

  • Need to specify your data source in context.xml file(Inside META-INF) :
  • <Context>
    <Resource 
    name="jdbc/AkWebAppDB" 
    auth="Container" 
    type="javax.sql.DataSource" 
    username="ak" 
    password="" 
    driverClassName="org.h2.Driver" 
    url="jdbc:h2:mem:target/test/db/h2/hibernate" 
    maxActive="8" 
    maxIdle="4"/>
    </Context>	
  • <resource-env-ref> inside web.xml which tells the container that you are using this resource :
  • <resource-env-ref>
    <resource-env-ref-name>jdbc/AkWebAppDB</resource-env-ref-name>
    <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
    </resource-env-ref>
    
  • persistence.xml situated inside META-INF :
  • <persistence-unit name="akwebapp">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
    <property name="hibernate.connection.datasource" value="java:comp/env/jdbc/AkWebAppDB"/>
    </properties>
    </persistence-unit>
    

Advertisements

Advertisement

Posted on: May 31, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: hibernate jndi  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0