升级到PostgreSQL 10之后c3po将无法连接,但DriverManager将会连接

时间:2018-05-24 04:43:34

标签: java postgresql jdbc c3p0

这很奇怪。我已经将这个C3PO代码用于Postgresql的4个版本,但现在我已升级到10并且它不会连接。

这会爆发

java.sql.SQLException: Connections could not be acquired from the underlying database!
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)

代码:

package com.wtf.pdssr;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class PostgresConnectionTest {
   DataSource cpds;

   @Test
   public void wtfTest() {
       try {
          cpds = getDatasource();
       } 
       catch (SQLException e) {
           e.printStackTrace();
       }
       assertNull(cpds);
       Connection conn = null;
       try {
           conn = getConnection();
           conn.close();
       } 
       catch (SQLException e) {
           e.printStackTrace();
       }
       assertNotNull(conn);
   }

// @Test

    public DataSource getDatasource() throws SQLException {
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        try {
            cpds.setDriverClass("org.postgresql.Driver");
        } catch (PropertyVetoException e) {
            throw new IllegalArgumentException(e);
        }
        String url = "jdbc:postgresql://localhost/sales_reporting_db";
        cpds.setJdbcUrl(url);
        cpds.setUser("jjs");
        cpds.setPassword("jjs");
        Connection conn = cpds.getConnection();
        conn.close();
        return cpds;
    }

    public Connection getConnection() throws SQLException {
       String url = "jdbc:postgresql://localhost/sales_reporting_db";
       Connection conn = DriverManager.getConnection(url,"jjs","jjs");
       return conn;
    }
}

0 个答案:

没有答案