我正在尝试将我的 jdbc 代码转换为在 Tomcat 上使用连接池功能。我的系统是一个连接到 SQL Server 2008 的 jsp/servlet 应用程序。所以,让我们来看看代码......
我的 Connection 类如下所示:
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
//... some more imports
public class DbPooledConnectionToMSSQL {
public String dbsource ;
private Connection dbCon;
private DataSource datasource = new DataSource();
private PoolProperties p = new PoolProperties();
public DbPooledConnectionToMSSQL() {
super();
dbsource = "jdbc:sqlserver://xxx.xxx.x.x:1433;database=xxx";
String user = "user";
String password = "pass";
p.setUrl(dbsource);
p.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//p.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource");
p.setUsername(user);
p.setPassword(password);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
datasource.setPoolProperties(p);
}
public boolean connect() throws ClassNotFoundException, SQLException {
try {
if (dbCon == null) {
System.err.println("Creating Pooled Connection....");
dbCon = datasource.getConnection(); //<-- here is the exception
System.err.println("!!! Pooled Connection creation OK");
} else {
System.err.println("!!! Connection EXIST not creation");
}
} catch (SQLException e) {
return false;
} catch (Exception e) {
return false;
}
return true;
}
//more code below
}
现在,在我的跑步者类中,我初始化了这个类并尝试连接到我的数据库,但是我在命令中得到了著名的 ClassNotFoundExceptiondbCon = datasource.getConnection();
我确信连接驱动程序已经到位,因为它已经在我的常规 jdbc 代码(不是连接池)中使用并且工作正常。
我也尝试com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource
用作驱动程序类,但我也得到了同样的异常:
java.sql.SQLException: com.microsoft.sqlserver.jdbc.SQLServerDriver
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.<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.DataSourceProxy.getConnection(DataSourceProxy.java:127)
at admin.db.DbPooledConnectionToMSSQL.connect(DbPooledConnectionToMSSQL.java:97)
......
Caused by: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
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)
我在这里做错了什么?是否有另一个 jdbc 驱动程序可以完成这项工作?