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

徘徊边缘 提交于 2019-12-11 00:22:47

问题


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?


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/5941523/if-i-create-a-spring-datasource-do-i-still-need-to-define-the-datasource-inside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!