SEVERE: unable to create initial pool connections - tomcat 7 with context.xml file

I tried to start the project on tomcat 7.0.52 and initialize to DB via context.xml file.

But he throws a bunch of exceptions, I could not understand what was wrong there.

Here is the console output:

 java.sql.SQLException: com.mysql.jdbc.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:701) at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:635) at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:486) at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(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:554) at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:242) at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:141) at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321) 

This is a complete stack trace .

Tomcat catalina log is displayed here.

snippet web.xml :

 <resource-ref> <description>Travel Agency Datasource</description> <res-ref-name>jdbc/onlinedb</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 

context.xml

 <Context> <Resource name="jdbc/onlinedb" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" maxActive="20" maxIdle="10" maxWait="-1" username="root" password="secret" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/travelagency?characterEncoding=utf8"/> </Context> 

ConnectionManager class:

 public class ConnectionManager { private static Logger log = Logger.getLogger(ConnectionManager.class); public static Connection getConnection() throws SQLException { Connection con = null; try { Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); DataSource datasource = (DataSource) envContext.lookup("jdbc/onlinedb"); con = datasource.getConnection(); } catch (NamingException e) { log.error(e); } return con; } } 

mysql-connector-java-5.1.27-bin.jar added to cp:

travel

I tried changing the contents of the context.xml file:

 <resource-env-ref> <description>Travel Agency Datasource</description> <resource-env-ref-name>jdbc/onlinedb</resource-env-ref-name> <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type> </resource-env-ref> 

But he continues to fail.

How to solve this problem?

+9
java xml tomcat runtimeexception
source share
4 answers

You need to add the MySQL jdbc driver to the classpath.

Place the MySQL binary jar in the tomcat lib folder or add it to the WEB-INF / lib folder.

You can find the binary jar (change the version accordingly): https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.27

+11
source share

When you encounter such exceptions, the most useful information is usually located at the bottom of the stacktrace:

 Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:366) ... at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246) 

The problem is that Tomcat cannot find com.mysql.jdbc.Driver . This is usually caused by a JAR containing the MySQL driver, which is not where Tomcat expects to find it (namely, in the webapps/<yourwebapp>/WEB-INF/lib ).

+4
source share

I also considered this exception after the fully working context.xml setting was configured. I do not need environmental data in context.xml, so I took them out and saw this error. I realized that I had to completely create this data source resource in the code based on the JVM -D args system properties.

Original error with remote user / pwd / host: org.apache.tomcat.jdbc.pool.ConnectionPool init SEVERE: Unable to create initial pool connections.

All context.xml contents have been deleted and try the following: Initialize the data source object when starting the application server before using the first connection. If you use Spring, it is useful to do this in the @Configuration bean in the @Bean Datasource constructor.

for use: org.apache.tomcat.jdbc.pool. *

 PoolProperties p = new PoolProperties(); p.setUrl(jdbcUrl); p.setDriverClassName(driverClass); p.setUsername(user); p.setPassword(pwd); p.setJmxEnabled(true); p.setTestWhileIdle(false); p.setTestOnBorrow(true); p.setValidationQuery("SELECT 1"); p.setTestOnReturn(false); p.setValidationInterval(30000); p.setValidationQueryTimeout(100); p.setTimeBetweenEvictionRunsMillis(30000); p.setMaxActive(100); p.setInitialSize(5); p.setMaxWait(10000); p.setRemoveAbandonedTimeout(60); p.setMinEvictableIdleTimeMillis(30000); p.setMinIdle(5); p.setLogAbandoned(true); p.setRemoveAbandoned(true); p.setJdbcInterceptors( "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+ "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"); org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(); ds.setPoolProperties(p); return ds; 
+2
source share

I am using sprint-boot (2.1.1) and the mysql version is 8.0.13. I add a dependency to pom, solve my problem.

  <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> 

MySQL Connector / J "link 8.0.13: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.13
MySQL Connector / J "All version link: https://mvnrepository.com/artifact/mysql/mysql-connector-java

0
source share

All Articles