【问题标题】:Tomcat Connection Pool - Factory SettingTomcat 连接池 - 出厂设置
【发布时间】:2012-10-14 16:26:09
【问题描述】:

我一直在通过this网站学习如何设置Tomcat的连接池。所以我做了一个context.xml文件,放到META-INF目录下。这就是我现在所拥有的。

<?xml version="1.0" encoding="UTF-8"?>

<Context>
    <Resource name="jdbc/gmustudent" auth="Container"
        type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
        username="root" password="root"
        url="jdbc:mysql://localhost:3306/official"
        maxActive="100" maxIdle="10" minIdle="5" initialSize="5" maxWait="10000" />
</Context>

但是我想指定工厂的类名。但是每次我添加这个属性

factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"

我的控制台出现大量错误。以下是其中一些,排名不分先后。

WARNING: Failed to register in JMX: javax.naming.NamingException: com.mysql.jdbc.Driver
WARNING: Unexpected exception resolving reference java.sql.SQLException: com.mysql.jdbc.Driver
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:gmustudent' did not find a matching property.

因此,当我不指定工厂时,该站点可以正常工作。但是当我这样做时,会抛出错误并且没有任何效果。我对堆栈溢出进行了一些研究,发现this 帖子。因此,我将我的 tomcat 版本从 7.0.11 更改为最新版本,但仍然出现错误。所以后来我想这可能是我的 server.xml 文件中与我的工厂的某种冲突,但我几乎没有经验来打这个电话。但这是我 server.xml 文件中的资源

<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>

那么这是否与我的 context.xml 文件中的工厂冲突?还是我在这里完全错了?简而言之,我想了解如何在我的 context.xml 文件中指定工厂而不会出现巨大错误。感谢您的阅读。

【问题讨论】:

    标签: java jdbc connection-pooling


    【解决方案1】:

    如果需要,您实际上可以使用 Spring 管理 Web 应用程序中的整个连接。下面是一个使用 PostgreSQL 的例子:

    <?xml version="1.0" encoding="windows-1252"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="org.postgresql.Driver"/>
            <property name="url" value="jdbc:postgresql://localhost/mydb"/>
            <property name="username" value="postgres"/>
            <property name="password" value="postgres"/>
        </bean>
    </beans>
    

    您可以将其放入 WEB-INF/classes 并使用以下方式加载它:

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/mycontext.xml");
    

    此时我什至不会费心让 Tomcat 管理它。

    【讨论】:

      猜你喜欢
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 2015-08-12
      相关资源
      最近更新 更多