8

我的 JAVA 程序中有以下代码,它允许我将文件中的数据复制到我的 Postgres 数据库中:

Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:####/myDb", 
                                                   "myuser", "mypassword"); 
CopyManager cm = new CopyManager((BaseConnection) con);
cm.copyIn("COPY prices FROM STDIN WITH DELIMITER AS ','", 
             new BufferedReader(new FileReader(filepath)), buffersize);

此代码工作正常,但我想使用连接池来管理我的连接,因为我有此代码运行许多文件。所以我使用了 C3P0

public static final ComboPooledDataSource cpds = new ComboPooledDataSource();

public class MyPooledConnection {
MyPooledConnection() throws PropertyVetoException {
    cpds.setDriverClass("org.postgresql.Driver"); 
    cpds.setJdbcUrl("jdbc:postgresql://localhost:5432/myStockDatabase"); 
    cpds.setUser("myUserName"); 
    cpds.setPassword("myPassword"); 
    cpds.setInitialPoolSize(4);
    cpds.setMinPoolSize(4);
    cpds.setMaxIdleTime(30);
    cpds.setMaxPoolSize(MAX_CONNECTIONS);
}

public static Connection getConnection() {
    return cpds.getConnection();
}
}

但是,当我从上面的连接池中获得连接并尝试将其与 CopyManager 一起使用时,如下例所示,代码不起作用

Connection pooled_con = MyPooledConnection.getConnection();
CopyManager cm = new CopyManager((BaseConnection) pooled_con);
cm.copyIn("COPY prices FROM STDIN WITH DELIMITER AS ','", 
             new BufferedReader(new FileReader(filepath)), buffersize);

我猜问题出在连接上,但我似乎无法弄清楚它有什么不同。我试过用 SQLException 和 IOException 来捕捉错误,但它也没有捕捉到。有没有人遇到过这个?

----更新----

感谢 a_horse_with_no_name 对此的指导。以下代码对我有用

// Cast the connection as a proxy connection
C3P0ProxyConnection proxycon = (C3P0ProxyConnection)cpds.getConnection();
try {

    // Pass the getCopyAPI (from PGConnection) to a method
    Method m = PGConnection.class.getMethod("getCopyAPI", new Class[]{});
    Object[] arg = new Object[] {};

    // Call rawConnectionOperation, passing the method, the raw_connection, 
    // and method parameters, and then cast as CopyManager
    CopyManager cm = (CopyManager) proxycon.rawConnectionOperation(m,
                                         C3P0ProxyConnection.RAW_CONNECTION,arg);
    cm.copyIn("COPY prices FROM STDIN WITH DELIMITER AS ','", new BufferedReader(new 
                                                FileReader(filepath)), buffersize);
} catch (NoSuchMethodException | IllegalAccessException 
                        | IllegalArgumentException | InvocationTargetException e) {
    // Deal with errors here
}
4

2 回答 2

4

池不给你“本机”连接,它总是分发一个代理对象:

从手册:

C3P0 将这些对象包装在代理后面,因此您不能将 C3P0 返回的连接或语句强制转换为特定于供应商的实现类

您可能无法通过 C3P0 使用 CopyManager。我不确定,但也许您可以使用此处描述的解决方法:http ://www.mchange.com/projects/c3p0/#raw_connection_ops

如果这不起作用,您可能想要使用不同的连接池(例如新的Tomcat 7 JDBC-Pool),它可以让您访问底层的本机连接。

于 2013-01-26T18:26:19.130 回答
0
CopyManager cm = new CopyManager(cpds.getConnection().unwrap(PgConnection.class))

为我工作。

于 2021-07-05T14:33:59.603 回答