hibernate jndi

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>