Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and Where to add JNDI for Hibernate?

I need the code to add JNDI name to achive connection pooling in hibernate. I have configured the connetion pooling in Jboss server with the JNDI name as "EmployeeDB"

How to configure it in hibernate.cfg.xml ??

Plez give me the code for hibernate.cfg.xml if i am using Hibernate 4 Final release.

like image 242
bali208 Avatar asked Mar 13 '12 01:03

bali208


People also ask

Does hibernate use JNDI?

Using hibernate in web application is very easy, all we need is to configure DataSource properties in hibernate configuration file. First of all we need to setup test database and JNDI DataSource in tomcat container.

Does spring boot use JNDI?

To use JDBC in a Spring Boot application, you can define a Liberty dataSource in the server. xml, identically to a Java EE application. The data source that is defined in server. xml can be looked up by JNDI by the jndiName attribute in the dataSource element.

What is the use of JNDI DataSource?

A JNDI DataSource object is a file that contains the configuration details necessary to connect to a database. The DataSource object must be registered on a JNDI server, where it is identified using a JNDI name. You can register your DataSource object directly on your application server via its JNDI service.


1 Answers

The datasource JDNI name configured in the Jboss server is specified by the properties hibernate.connection.datasource.

The basic hibernate.cfg.xml should look like :

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration> 
    <session-factory>

        <!-- Database connection settings -->
        <property name="hibernate.connection.datasource">java:comp/env/jdbc/EmployeeDB</property>

        <!-- SQL dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Mapped annotated entity-->
        <mapping class="org.hibernate.tutorial.domain.Event"/>

    </session-factory> 
</hibernate-configuration>
like image 191
Ken Chan Avatar answered Nov 02 '22 07:11

Ken Chan