0

我一直在通过这个网站学习如何设置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.

因此,当我不指定工厂时,该站点可以正常工作。但是当我这样做时,会抛出错误并且没有任何效果。我对堆栈溢出做了一些研究,发现了这篇文章。因此,我将我的 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 文件中指定工厂而不会出现巨大错误。感谢您的阅读。

4

1 回答 1

0

如果需要,您实际上可以使用 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 管理它。

于 2012-10-24T15:35:44.997 回答