Home > Database, Java > Configuring Glassfish, Spring, Hibernate using JNDI

Configuring Glassfish, Spring, Hibernate using JNDI

I got a question from my mate, how to configure and integrate Spring, hibernate using datasources provide by glassfish server. in this case, using IBM db2 database.

below is the practical and simple step how create glassfish datasource, configure spring connection using current datasource

Configure Glassfish datasource

1. Copy jdbc driver of IBM db2 into ${glassfish_home_directory/lib} (db2jcc.jar)

2. start glassfish server and login using administrator user created during installation step of glasshfish server

3.choose JDBC Connection pools and create new one, fill all parameter, url, username and password

4. in this case, we create TestPool as new connection pool

5. then create JDBC Resources (i.e TestResources), Fill the jndi name and select TestPool as Pool Name, then save.

 

Configure Spring Connection:

1. Create bean dataSource

<!– data source –>

<bean id=”dataSource” class=”org.springframework.jndi.JndiObjectFactoryBean” destroy-method=”close”>

<property name=”jndiName” value=”TestResources” />

<property name=”lookupOnStartup” value=”false”/>

<property name=”cache” value=”true”/>

<property name=”proxyInterface” value=”javax.sql.DataSource”/>

</bean>

2. Create sessionFactory bean and provide dataSource as parameter ref

<bean id=”sessionFactory”

class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>

<property name=”dataSource” ref=”dataSource” />

<property name=”hibernateProperties”>

<props>

<prop key=”hibernate.default_schema” >${hibernate.schema}</prop>

<prop key=”hibernate.show_sql”>${hibernate.show_sql}</prop>

<prop key=”hibernate.show_statistics”>${hibernate.show_statistics}</prop>

<prop key=”hibernate.dialect”>${hibernate.dialect}</prop>

</props>

</property>

………

</bean>

3. Complete configuration should be like this one

<bean id=”dataSource” class=”org.springframework.jndi.JndiObjectFactoryBean” destroy-method=”close”>

<property name=”jndiName” value=”TestResources” />

<property name=”lookupOnStartup” value=”false”/>

<property name=”cache” value=”true”/>

<property name=”proxyInterface” value=”javax.sql.DataSource”/>

</bean>

<!– session factory –>

<bean id=”sessionFactory”

class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>

<property name=”dataSource” ref=”dataSource” />

<property name=”hibernateProperties”>

<props>

<prop key=”hibernate.default_schema” >askes01</prop>

<prop key=”hibernate.show_sql”>${hibernate.show_sql}</prop>

<prop key=”hibernate.show_statistics”>${hibernate.show_statistics}</prop>

<prop key=”hibernate.dialect”>${hibernate.dialect}</prop>

</props>

</property>

……

</bean>

That’s all

This configuration running well and tested on Glassfish 3.1

 

Saturday, Dec, 17th 2011

 

A. Ahmad Kusumah

 

 

Advertisement
Categories: Database, Java
  1. December 17, 2011 at 5:01 pm | #1

    Wowww… :D
    nice tutorial…..

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.