ETL tools completed almost, today, encountered a problem, is to C3p0 configured maxpoolsize for 10, the purpose is to let the entire application at the same time to obtain the maximum number of connection is 10, but after testing this part of the application, There are far more than 10 links found on the PostgreSQL side. Because the tool is multi-threaded, so I think, is not a multi-threaded problem, check the number of connection, it is indeed the number of 10* threads. Then a test was made:
To configure Maxpoolsize to 5, run the following program:
Combopooleddatasource CPDs = new Combopooleddatasource ("Postgres"); for (int i = 0; i <; i++) {cpds.getconnection ();} Combopooleddatasource cpds2 = new Combopooleddatasource ("Postgres"); for (int i = 0; i <; i++) {cpds2.getconnection () ;}
Maxpoolsize configured to 5, altogether want to obtain 20 links, at this time to see the server side of the PostgreSQL connection number is 5, the official expected results.
Under Run the following program:
for (int i = 0; i < 2; i++) {New Thread (new Runnable () {@Overridepublic void run () {Combopooleddatasource CPDs = new Co Mbopooleddatasource ("Postgres"); for (int i = 0; i <; i++) {try {cpds.getconnection ();} catch (SQLException e) {E.pri Ntstacktrace ();}} try {thread.sleep (100000);} catch (Interruptedexception e) {e.printstacktrace ();}}}, "Thread" + i). Start ();
Two threads, each thread wants to get 10 links, the number of links on the PostgreSQL server side is 10. Change I<2 to I<3, run once, see the number of connections on the PostgreSQL server side is 15, you can determine
PostgreSQL server-side connection number =maxpoolsize*numthread;
The underlying reason is probably because C3P0 is not designed for multithreading, and the underlying JDBC is not thread-safe. Specific reasons, there are opportunities in specific analysis.
C3P0 maxpoolsize configuration under Multi-threading