Marco
Marco

Reputation: 15929

If i create a Spring datasource, do i still need to define the datasource inside Tomcat context.xml?

currently we have an application that is configured by using a datasource inside a Tomcat context.xml file. So we are able to succesfully get a connection by retrieving the JNDI name and get a connection. I was wondering if we could replace this by using a Spring datasource and if we still need the information inside the context.xml file?

Example 'context.xml':

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource
        name="jdbc/myDataSource"
        auth="Container"
        type="javax.sql.DataSource"
        username="john"
        password="doe"
        driverClassName="<removed>"
        url="<removed>"
        maxActive="30"
        maxIdle="10"
        maxWait="1000"
        removeAbandonedTimeout="60"
        removeAbandoned="true"
        logAbandoned="true"/>
</Context>

So in our code we search for a JNDI context like this:

Context envCtx = (Context) initCtx.lookup(..);
DataSource ds = (DataSource) envCtx.lookup(..);
Connection connection = ds.getConnection();

I was wondering if we could better define a Spring Datasource instead of using this approach and how we could do this?

Upvotes: 2

Views: 2182

Answers (3)

kiwiron
kiwiron

Reputation: 1705

Putting your datasource definition in Spring configuration files usually means the code needs to be recompiled when moved between development, test and production environments. Putting the definition in context.xml means the same binary will work in any environment.

Upvotes: 1

Clint Eastwood
Clint Eastwood

Reputation: 5698

The question might have already been answered by Tomasz Nurkiewicz but I wanted to point something out regarding commons-dbcp vs. tomcat jdbc connection pool: the latter has several advantages over the portable and more generic commons-dbcp, one of the most important ones: tomcat connection pool might be much faster and prevent starvation. Here's more info that might be useful to read before making a decision: http://people.apache.org/~fhanik/jdbc-pool/jdbc-pool.html

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

I assume your dataSource is now configured similar to this:

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

If you replace it with something like this:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://${jdbc.hostname}/${jdbc.schema}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

DataSource configuration in Tomcat is no longer needed.

In the first case connection pool is managed and exposed by Tomcat using its own implementation. The latter configuration (which I would strongly recommend due to portability and cutting down dependencies to container) does not rely on Tomcat. Instead, Spring instantiates its own connection pool (note that pool implementation comes from external library like DBCP or C3P0) and there is absolutely no reference to Tomcat JNDI reference.

Upvotes: 6

Related Questions