I'd like to know if it's possible to share a database connection pool bean (set up by Spring) and share it between two WARs that are deployed in JBoss.

For example, I'd like something like this: database-pool.xml

    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>

WAR app1's applicationContext.xml:

    <import resource="database-pool.xml"/>
    <bean id="someBean1">
        <property name="datasource" ref="datasource"/>
    </bean>

WAR app2's applicationContext.xml:

    <import resource="database-pool.xml"/>
    <bean id="someBean2">
        <property name="datasource" ref="datasource"/>
    </bean>

My ultimate goal here is to have a single instance of the database connection pool. Is that feasible?

有帮助吗?

解决方案

I think it will be hard (if not impossible) if you use app-local datasource. You can however share it if you install your datasource on the container (see Chapter 17. Datasource Configuration -- JBoss 5 reference). Container datasource will have a JNDI name which you can inject to Spring container using <jee:jndi-lookup> tag:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/>

JNDI Datasource can be deployed not only administratively (admin console), but programmatic as well. Each war could have following code logic on Spring container initialization:

  1. Check if there exist a datasource with JNDI name "jdbc/MyDatasource".
  2. If doesn't exist, create one and deploy.
  3. If exist, just grab the reference, place it into Spring container.

However the weakness of this method is if you do have to change the datasource property (server ip / database name) later in the future you have to update and recompile every single war.

Configuring datasource on the container using admin console is a more popular approach in this regard.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top