[JAVA EE][Tomcat] JDBC CP zamiast Commons DBCP


(kostek135) #1

Witam, korzystam z Tomcat 7.0.42 i PostrgeSQL 9.2.

Uruchomiłem Tomcata z domyślną pulą połączeń Commons DBCP poprzez:

1.Dodanie drivera JDBC dla Postgres'a do katalogu lib w WEB-INF

2.Dodanie wpisu pliku context.xml konfigurującego zasób:

  1. Dodanie do pliku web.xml odniesienia do zasobu

    postgreSQL Datasource example
    jdbc/postgres
    javax.sql.DataSource
    Container

  2. No i finalnie skorzystanie z niego

    ...

    Context initContext = new InitialContext();

    Context envContext = (Context) initContext.lookup("java:/comp/env");

    dataSource = (DataSource) envContext.lookup("jdbc/postgres");

    Connection con = dataSource.getConnection();

    Statement stmt = con.createStatement();

    ResultSet rs= stmt.executeQuery("select * from persons");

    while(rs.next()) {

    ...

    }

Wszystko działa jest tylko jedno ale, w dokumentacji Tomcata napisali dlaczego nie korzystać z implementacji Commons-DBCP, a zamiast tego przejść na JDBC Connection Pool. Napisali, też, że to proste (ale chyba nie do końca, albo coś pominąłem)

dodałem do pierwszego pliku (context.xml) do tagu resource atrybut

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

, ale wtedy Tomcat odmawia załadowania drivera JDBC przy uruchamianiu. Czy ktoś wie jak to rozwiązać? PS Poniżej Stack Trace, który został wyprodukowany

lip 31, 2013 9:11:23 PM org.apache.catalina.core.AprLifecycleListener init

INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jdk1.7.0_25\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files (x86)/Java/jre7/bin/client;C:/Program Files (x86)/Java/jre7/bin;C:/Program Files (x86)/Java/jre7/lib/i386;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files (x86)\eclipse for jee;;.

lip 31, 2013 9:11:24 PM org.apache.tomcat.util.digester.SetPropertiesRule begin

WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SimpleREST' did not find a matching property.

lip 31, 2013 9:11:24 PM org.apache.tomcat.util.digester.SetPropertiesRule begin

WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:MyServlet' did not find a matching property.

lip 31, 2013 9:11:24 PM org.apache.coyote.AbstractProtocol init

INFO: Initializing ProtocolHandler ["http-bio-8080"]

lip 31, 2013 9:11:24 PM org.apache.coyote.AbstractProtocol init

INFO: Initializing ProtocolHandler ["ajp-bio-8009"]

lip 31, 2013 9:11:24 PM org.apache.catalina.startup.Catalina load

INFO: Initialization processed in 1336 ms

lip 31, 2013 9:11:24 PM org.apache.catalina.core.StandardService startInternal

INFO: Starting service Catalina

lip 31, 2013 9:11:24 PM org.apache.catalina.core.StandardEngine startInternal

INFO: Starting Servlet Engine: Apache Tomcat/7.0.42

lip 31, 2013 9:11:24 PM org.apache.naming.NamingContext lookup

WARNING: Unexpected exception resolving reference

java.sql.SQLException: org.postgresql.Driver

	at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254)

	at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:702)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:634)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:488)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.(ConnectionPool.java:144)

	at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116)

	at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103)

	at org.apache.tomcat.jdbc.pool.DataSourceFactory.createDataSource(DataSourceFactory.java:539)

	at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:237)

	at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:143)

	at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:843)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:154)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:831)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:168)

	at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1093)

	at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:672)

	at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:271)

	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)

	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)

	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)

	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)

	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)

	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)

	at java.util.concurrent.FutureTask.run(FutureTask.java:166)

	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

	at java.lang.Thread.run(Thread.java:724)

Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver

	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

	at java.security.AccessController.doPrivileged(Native Method)

	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

	at java.lang.Class.forName0(Native Method)

	at java.lang.Class.forName(Class.java:270)

	at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)

	... 29 more


lip 31, 2013 9:11:24 PM org.apache.catalina.core.NamingContextListener addResource

WARNING: Failed to register in JMX: javax.naming.NamingException: org.postgresql.Driver

lip 31, 2013 9:11:25 PM org.apache.naming.NamingContext lookup

WARNING: Unexpected exception resolving reference

java.sql.SQLException: org.postgresql.Driver

	at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254)

	at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:702)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:634)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:488)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.(ConnectionPool.java:144)

	at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116)

	at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103)

	at org.apache.tomcat.jdbc.pool.DataSourceFactory.createDataSource(DataSourceFactory.java:539)

	at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:237)

	at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:143)

	at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:843)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:154)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:831)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:168)

	at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1093)

	at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:672)

	at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:271)

	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)

	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)

	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)

	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)

	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)

	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)

	at java.util.concurrent.FutureTask.run(FutureTask.java:166)

	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

	at java.lang.Thread.run(Thread.java:724)

Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver

	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

	at java.security.AccessController.doPrivileged(Native Method)

	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

	at java.lang.Class.forName0(Native Method)

	at java.lang.Class.forName(Class.java:270)

	at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)

	... 29 more


lip 31, 2013 9:11:25 PM org.apache.catalina.core.NamingContextListener addResource

WARNING: Failed to register in JMX: javax.naming.NamingException: org.postgresql.Driver

lip 31, 2013 9:11:25 PM org.apache.naming.NamingContext lookup

WARNING: Unexpected exception resolving reference

java.sql.SQLException: org.postgresql.Driver

	at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254)

	at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:702)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:634)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:488)

	at org.apache.tomcat.jdbc.pool.ConnectionPool.(ConnectionPool.java:144)

	at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116)

	at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103)

	at org.apache.tomcat.jdbc.pool.DataSourceFactory.createDataSource(DataSourceFactory.java:539)

	at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:237)

	at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:143)

	at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:843)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:154)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:831)

	at org.apache.naming.NamingContext.lookup(NamingContext.java:168)

	at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1093)

	at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:672)

	at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:271)

	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)

	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)

	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)

	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)

	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)

	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)

	at java.util.concurrent.FutureTask.run(FutureTask.java:166)

	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

	at java.lang.Thread.run(Thread.java:724)

Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver

	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

	at java.security.AccessController.doPrivileged(Native Method)

	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

	at java.lang.Class.forName0(Native Method)

	at java.lang.Class.forName(Class.java:270)

	at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)

	... 29 more


lip 31, 2013 9:11:25 PM org.apache.catalina.core.NamingContextListener addResource

WARNING: Failed to register in JMX: javax.naming.NamingException: org.postgresql.Driver

lip 31, 2013 9:11:25 PM org.apache.coyote.AbstractProtocol start

INFO: Starting ProtocolHandler ["http-bio-8080"]

lip 31, 2013 9:11:25 PM org.apache.coyote.AbstractProtocol start

INFO: Starting ProtocolHandler ["ajp-bio-8009"]

lip 31, 2013 9:11:25 PM org.apache.catalina.startup.Catalina start

INFO: Server startup in 1284 ms

-- Dodane 31.07.2013 (Śr) 23:06 --

Znalazłem. Strasznie głupi problem. Ale zostawie notkę dla osób, które będą miały podobny "błąd".

Dodałem zasób globalnie - przez co był ładowany dla każdego servletu. Poza tym tylko ostrzegał (WARNING) w przeciwieństwie do implementacji Commons, że nie można załadować driver'a, a sam błąd miał źródło w tym, że były dwa servlety (jeden z nich nie korzystał z zasobu). Myślałem, że zamknięcie projektu przestanie dodawać deploy'ować servlet, tymczasem ono i tak to robiło. Wystarczy go wyłączyć (jego deployment) w konfiguracji serwera, albo dodać zasób lokalnie dla danego servletu.

Temat można zamknąć