如果我创建一个Spring数据源,是否还需要在Tomcat context.xml中定义数据源? [英] If i create a Spring datasource, do i still need to define the datasource inside Tomcat context.xml?

查看:208
本文介绍了如果我创建一个Spring数据源,是否还需要在Tomcat context.xml中定义数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们有一个使用Tomcat context.xml文件中的数据源配置的应用程序.因此,我们能够通过检索JNDI名称并获得连接来成功获得连接.我想知道我们是否可以使用Spring数据源替换它,是否仍然需要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?

示例"context.xml":

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>

因此,在我们的代码中,我们搜索这样的JNDI上下文:

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();

我想知道我们是否可以更好地定义Spring Datasource而不是使用这种方法,以及我们如何做到这一点?

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

推荐答案

我假设您的dataSource现在的配置与此类似:

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>

不再需要Tomcat 中的

DataSource配置.

DataSource configuration in Tomcat is no longer needed.

在第一种情况下,连接池由Tomcat使用其自己的实现进行管理和公开.后一种配置(由于可移植性和减少对容器的依赖性而强烈建议使用)不依赖于Tomcat.相反,Spring实例化了自己的连接池(请注意,池的实现来自外部库,例如DBCP或C3P0),并且绝对没有对Tomcat JNDI参考的引用.

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.

这篇关于如果我创建一个Spring数据源,是否还需要在Tomcat context.xml中定义数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆