简体   繁体   中英

Jdbc Connection Pool with Sql Server 2008 fails

I am trying to turn my jdbc code to use Connection Pool functionality on Tomcat. My system is a jsp/servlet application that connects to an SQL Server 2008. So, lets get to the code...

My Connection class looks like this:

    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
}

Now, in my runner class I initialize this class and try to connect to my database, but I get the famous ClassNotFoundException on the command dbCon = datasource.getConnection();

I am sure that the Connection Driver is in place because it is already used in my regular jdbc code (not the Connection Pooling) and works just fine.

I also tried to use com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource as driver class but I also get the same exception:

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)

What do I do wrong here? Is there another jdbc Driver that could do the job?

Ok, found the problem and the answer is pretty simple. Looking at the specification for PoolProperties , it says that :

setDriverClassName(): The fully qualified Java class name of the JDBC driver to be used. The driver has to be accessible from the same classloader as tomcat-jdbc.jar

So even the driver was accessible by my application, it was not accessible by Tomcat. So putting the driver in $CATALINA_HOME/libs directory and restarting Tomcat solved this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM