我正在尝试更改应用程序的连接池,以便使用tomcat的连接池(org.apache.tomcat.jdbc.pool(而不是" apache commons dbcp"。
但是,尝试连接到DB时我会遇到此错误:
javax.servlet.ServletException: java.lang.AbstractMethodError: Method com/mysql/jdbc/PreparedStatement.isClosed()Z is abstract
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
...
...
我已经在其他链接中阅读了通常这是MySQL-JDBC驱动程序版本的问题,但是,我刚刚更新到最新的连接器/J版本(MySQL-Connector-java-8.8.0.11.jar(但是我仍然遇到这个错误。
这是创建连接池的方式:
首先,这在我的应用的元I-Inf目录中的context.xml文件上进行:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="rhwebDB"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="30000"
maxActive="10"
minIdle="5"
maxIdle="10"
maxWait="10000"
initialSize="2"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
username="dbUser"
password="dbPwd"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/rhweb2015"/>
</Context>
然后,我有一个其他库都使用的dbutil.class:
public class DBUtil {
static Connection connection = null;
static {
try {
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/rhwebDB");
connection = ds.getConnection();
} catch (NamingException e) {
System.out.println("DBUtil.NamingException" + e);
} catch (SQLException e) {
System.out.println("DBUtil.SQLException" + e);
}
}
public static Connection getConnection() {
return connection;
}
public static synchronized void closeConnection(Connection conn) {
try {
if (conn != null && !conn.isClosed())
conn.close();
} catch (SQLException sqle) {
System.out.println("Error closing the connection ! " + sqle);
}
}
}
最后,所有其他Java库都使用这样的连接池:
public boolean someFunction( String myValue ){
Connection conn = null;
boolean fRetVal = false;
String query = "select something from anytable";
try {
conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery( query );
if ( rs.next() )
fRetVal = true;
rs.close();
stmt.close();
} catch( SQLException ex ) {
System.out.println(ex);
}finally{
DBUtil.closeConnection(conn);
}
return fRetVal;
}
我想念什么吗?tomcat启动时我不会遇到任何错误。
JDK版本:8(Java版本" 1.8.0_111"(Tomcat版本:8.5.8MySQL Server 5.6
任何帮助都将不胜感激
这都是错误的。您正在养狗并吠叫。首先,您不能使用静态Connections
,第二位是使用静态Connection
击败使用连接池的整个目的。应该更像:
public class DBUtil {
static DataSource ds;
static {
try {
Context context = new InitialContext();
ds = (DataSource)context.lookup("java:comp/env/rhwebDB");
} catch (NamingException e) {
System.out.println("DBUtil.NamingException" + e);
} catch (SQLException e) {
System.out.println("DBUtil.SQLException" + e);
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
,然后使用try-with-resources关闭所有内容:
public boolean someFunction( String myValue ){
boolean fRetVal = false;
String query = "select something from anytable";
try (Connection conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery( query )) {
if ( rs.next() )
fRetVal = true;
} catch( SQLException ex ) {
System.out.println(ex);
}
return fRetVal;
}
e&amp; oe
但请注意,您实际上根本不需要DBUtil
类。您可以通过注释将DataSource
注入所需的任何地方。