Java 严重:无法使用 context.xml 文件创建池的初始连接 - tomcat 7
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22155837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SEVERE: Unable to create initial connections of pool - tomcat 7 with context.xml file
提问by nazar_art
I tried to run project on tomcat 7.0.52
and initialize to DB through context.xml
file.
我试图在 tomcat 上运行项目7.0.52
并通过context.xml
文件初始化到 DB 。
But it throws bunch of exceptions, I couldn't figure out what is wrong there.
但它引发了一堆异常,我无法弄清楚那里出了什么问题。
Here is 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)
Here is full stack trace.
这是完整的堆栈跟踪。
Here is output to tomcat catalina log
snippet of web.xml:
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:
上下文.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>
ConnectionManagerclass:
连接管理器类:
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
is added to cp:
mysql-connector-java-5.1.27-bin.jar
添加到cp:
I tried to change content of context.xml file:
我试图更改 context.xml 文件的内容:
<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 it keep failing.
但它一直失败。
How to solve this trouble?
如何解决这个烦恼?
采纳答案by WeMakeSoftware
You have to add a MySQL jdbc driver to the classpath.
您必须将 MySQL jdbc 驱动程序添加到类路径。
Either put a MySQL binary jar to tomcat libfolder or add it to we application WEB-INF/libfolder.
要么将 MySQL 二进制 jar 放入tomcat lib文件夹,要么将其添加到我们的应用程序WEB-INF/lib文件夹中。
You can find binary jar (Change version accordingly): https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.27
您可以找到二进制 jar(相应地更改版本):https: //mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.27
回答by matt b
When you encounter exceptions like this, the most useful information is generally at the bottom of the stacktrace:
当你遇到这样的异常时,最有用的信息通常在堆栈跟踪的底部:
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.run(URLClassLoader.java:366)
...
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)
The problem is that Tomcat can't find com.mysql.jdbc.Driver
. This is usually caused by the JAR containing the MySQL driver not being where Tomcat expects to find it (namely in the webapps/<yourwebapp>/WEB-INF/lib
directory).
问题是 Tomcat 找不到com.mysql.jdbc.Driver
. 这通常是由于包含 MySQL 驱动程序的 JAR 不在 Tomcat 期望的位置(即在webapps/<yourwebapp>/WEB-INF/lib
目录中)引起的。
回答by bjm88
I have also dealt with this exception after a fully working context.xml setup was adjusted. I didn't want environment details in the context.xml, so I took them out and saw this error. I realized I must fully create this datasource resource in code based on System Property JVM -D args.
在调整了完全工作的 context.xml 设置后,我也处理了这个异常。我不想在context.xml 里有环境细节,所以我把它们拿出来,看到了这个错误。我意识到我必须在基于 System Property JVM -D args 的代码中完全创建这个数据源资源。
Original error with just user/pwd/host removed: org.apache.tomcat.jdbc.pool.ConnectionPool init SEVERE: Unable to create initial connections of pool.
仅删除 user/pwd/host 的原始错误:org.apache.tomcat.jdbc.pool.ConnectionPool init SEVERE:无法创建池的初始连接。
Removed entire contents of context.xml and try this: Initialize on startup of app server the datasource object sometime before using first connection. If using Spring this is good to do in an @Configuration bean in @Bean Datasource constructor.
删除 context.xml 的全部内容并尝试以下操作:在使用第一次连接之前的某个时间在应用服务器启动时初始化数据源对象。如果使用 Spring,最好在 @Bean 数据源构造函数中的 @Configuration bean 中执行此操作。
package to use: org.apache.tomcat.jdbc.pool.*
要使用的包: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;
回答by Zgpeace
I use sprint-boot (2.1.1), and mysql version is 8.0.13. I add dependency in pom, solve my problem.
我使用 sprint-boot (2.1.1),mysql 版本是 8.0.13。我在 pom 中添加了依赖项,解决了我的问题。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
MySQL Connector/J ? 8.0.13 link: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.13
MySQL Connector/J ? All the version link:
https://mvnrepository.com/artifact/mysql/mysql-connector-java
MySQL 连接器/J ? 8.0.13 链接:https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.13
MySQL Connector/J ? 所有版本链接:https:
//mvnrepository.com/artifact/mysql/mysql-connector-java