听说tomcat 7 有了新的jdbc pool 性能不错,我就参照网上的资料配置了下,结果报错了。
server.xml增加:
<Resource name="jdbc/test" auth="Container"
type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true" testOnBorrow="true" testOnReturn="false"
validationInterval="30000" timeBetweenEvictionRunsMillis="30000" maxActive="100"
minIdle="10" maxWait="10000" initialSize="10" removeAbandonedTimeout="60"
removeAbandoned="true" logAbandoned="true" minEvictableIdleTimeMillis="30000"
jmxEnabled="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
username="root" password="123" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"/>
然后 context.xml 增加:
<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/>
代码中这么写:
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
//省略部分代码
private Connection initConnection(){
try {
context = new InitialContext();
} catch (NamingException e) {
System.err.println("连接池上下文失败 " + e.getMessage());
}
try {
ds = (DataSource)context.lookup("java:comp/env/jdbc/test");
} catch (NamingException e) {
System.err.println("数据源不存在 " + e.getMessage());
}
结果报错了,错误
WARNING: Unexpected exception resolving reference
java.sql.SQLException: com.mysql.jdbc.Driver
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
数据源不存在! com.mysql.jdbc.Driver
确认jar包是存在的。
什么原因,求大神帮助。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
看异常是驱动没有加载到,你是不是把mysql驱动包放到了 /WEB-INF/lib 中了,这对于tomcat的common类加载器来说是加载不到的,你可以试试把驱动包放到tomcat的lib目录下。
你好,我是AI助理
可以解答问题、推荐解决方案等
评论
全部评论 (0)