Marketplace for Content, Tests and Assessment
 
 
Home > Programming / tutorials > Spring Application and Jforum Integration – Sharing same c3p pool

Spring Application and Jforum Integration – Sharing same c3p pool

I hope you have gone through my previous blog about Jforum integration. There I have explained integration of Jforum into a spring / jsf application.

After integration the main problem was the database connectivity. My spring application was using a c3p pool datasource and the Jforum was also creating c3p pool and datasources to connect to db. As I had all tables (both Jforum and my application tables) in same db, it was not appropriate to have 2 pools for same db by same application.

I had following in my spring’s applicationContext.xml file:

 <bean id=”dataSource” destroy-method=”close”>

<property name=”driverClass” value=”${db.driverClass}”/>

<property name=”jdbcUrl” value=”${db.url}”/>

<property name=”user” value=”${db.username}”/>

<property name=”password” value=”${db.password}”/>

<property name=”minPoolSize” value=”2″/>

<property name=”maxPoolSize” value=”10″/>

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

<property name=”acquireRetryAttempts” value=”3″/>

<property name=”idleConnectionTestPeriod” value=”300″/>

</bean>

Spring initializes this pool when application loads. The JForum uses com.jforum.C3P0PooledConnectionclass to create pool for mysql db. It has init() method which create the pool and initiates ComboPooledDataSource intstance. JForum then uses it throughout the life cycle.

My goal was to make JForum to use spring initiated pool.

It can be done as follows:

Create a class implementing ApplicationContextAware interface.

As below:

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

public class SpringApplicationContext implements ApplicationContextAware {

private static ApplicationContext CONTEXT;

public void setApplicationContext(ApplicationContext context) throws BeansException {

CONTEXT = context;

public static Object getBean(String beanName) {

return CONTEXT.getBean(beanName);

}

}

Using this, we should be able to get the spring managed beans, which spring initializes during startup.   Calling getBean() method and sending it bean id as argument, we can get the spring managed instance.

And now replace the init() method of net.jforum. C3P0PooledConnection like below:

public void init() throws Exception

{

ds =(ComboPooledDataSource)  SpringApplicationContext.getBean(“dataSource”);

System.out.println(“datasource ==>”+ds);

}

That’s it. You are done. Now JSF uses the spring’s c3p pool.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes