简体   繁体   中英

Tomcat Connection Pool - Factory Setting

I have been learning about how to set up Tomcat's connection pool through this website. So I made a context.xml file and put it in the directory META-INF. This is what I have right now.

<?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>

However I would like to specify the class name of the factory. But everytime I add this attribute

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

I get a huge pile of errors in my console. Here are some of them in no particular order.

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.

So when I do not specify a factory the site works just fine. But when I do errors are thrown and nothing works. I did some research on stack overflow and found this post. Because of this I changed my tomcat version from 7.0.11 to the most recent but am still getting the error. So then I thought maybe it is some kind of clash with my factory in my server.xml file but I am not nearly experienced to make that call. But here is the resource in my server.xml file

<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"/>

So is this clashing with the factory in my context.xml file? Or am I totally wrong here? In a nut shell I would like to find out how to specify a factory in my context.xml file without getting a huge error. Thank you for reading.

You can actually manage the entire connection in your web app using Spring if you want. Here is an example using 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>

You can just put that in WEB-INF/classes and load it using:

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

I wouldn't even bother with having Tomcat manage it at this point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM