Postgres CopyManager and connection from Connection Pool

Keywords: Big Data Java PostgreSQL SQL Database

1. PG CopyManager use sample code:

package test.simple;
 
//You need to include postgres jdbc jar into your project lib
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
 
import java.io.FileInputStream;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import java.io.File;
 
/**
 * Created with IntelliJ IDEA.
 * User: leon
 * Date: 13-2-5
 * Time: 12:18 p.m.
 * To change this template use File | Settings | File Templates.
 */
public class TestCopy {
    public static void main(String args[])throws Exception{
        Class.forName("org.postgresql.Driver");
        String url = "jdbc:postgresql://localhost:5432/postgres";
        Properties props = new Properties();
        props.setProperty("user", "postgres");
        props.setProperty("password", "postgres");
        Connection conn = DriverManager.getConnection(url, props);
        CopyManager cm = new CopyManager((BaseConnection)conn);
        File file = new File("a.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        /*FileWriter fw = new FileWriter(file);
        cm.copyOut("COPY test_delete TO STDOUT WITH DELIMITER AS '|'", fw);
        fw.close();*/
 
        FileInputStream fis = new FileInputStream(file);
        cm.copyIn("COPY test_delete FROM STDIN WITH DELIMITER AS '|'", fis);
        fis.close();
    }
}

Very simple, the table data export to the txt file, you can add one step to compress the exported data file, can greatly reduce the disk space occupied by the file.


2. connection from Connection Pool

Unfortunately, CopyManager is a unique feature of PG. If you use CopyManager, you must get it from BaseConnection (that is, the Connection of PG). The database connection pool is usually used in actual projects.
Consider that connections taken from the PG connection pool cannot be forced to convert to BaseConnection.
Look at the simple PG connection pool code and turn one of the connections into a BaseConnection error:

import java.sql.Connection;
 
import org.postgresql.core.BaseConnection;
import org.postgresql.ds.PGPoolingDataSource;
public class PGPool {
    PGPoolingDataSource source = new PGPoolingDataSource();
    public PGPool(){
        source.setServerName("localhost");
        source.setPortNumber(5432);
        source.setDatabaseName("postgres");
        source.setUser("postgres");
        source.setPassword("postgres");
        source.setMaxConnections(10);
    }
 
    public Connection getConn(){
        try{
            return source.getConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
 
    public static void main(String args[]){
        PGPool pool = new PGPool();
        Connection con = pool.getConn();
        BaseConnection bConn = (BaseConnection) con;
    }
}
Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to org.postgresql.core.BaseConnection
	at PGPool.main(PGPool.java:35)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Complete code samples such as the following (get a connection from the connection pool and connect to the CopyManager operation of the PG):

/**
 * Created with IntelliJ IDEA.
 * User: leon
 * Date: 13-2-12
 * Time: 8:20 p.m.
 * To change this template use File | Settings | File Templates.
 */
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
//Remember, You need to include PG jdbc jar into your project build path.
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
import org.postgresql.ds.PGPoolingDataSource;
public class PGPool {
    PGPoolingDataSource source = new PGPoolingDataSource();
    public PGPool(){
        //PG database server name
        source.setServerName("localhost");
        //PG db port number
        source.setPortNumber(5432);
 
        //PG database name
        source.setDatabaseName("postgres");
        source.setUser("postgres");
        source.setPassword("postgres");
        source.setMaxConnections(10);
    }
 
    public Connection getConn(){
        try{
            return source.getConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
 
    public static void main(String args[]) throws Exception{
        PGPool pool = new PGPool();
        Connection con = pool.getConn();
 
        CopyManager cm = new CopyManager((BaseConnection)con.getMetaData().getConnection());
        File file = new File("a.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        FileInputStream fis = new FileInputStream(file);
        //According to our simple example, your should have a table named test_delete in your database
        cm.copyIn("COPY test_delete FROM STDIN WITH DELIMITER AS '|'", fis);
        fis.close();
    }
}

Change from: https://blog.csdn.net/leonskenedy/article/details/8579564

Posted by mattyj10 on Sat, 02 Feb 2019 09:09:16 -0800