Archive

Archive for December, 2011

Configuring Glassfish, Spring, Hibernate using JNDI

December 17, 2011 1 comment

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

 

 

Categories: Database, Java Tags: , ,