I create a pool of connections, but mysql swears at a large number of connections, as I understand it is necessary to close the connection after its use. But isn't a connection pool created for this purpose, to keep a certain number of connections for quick access to them from different streams? Please tell me what I'm doing wrong, maybe I misunderstand the mechanism of the pool of connections?

package ObjectPool; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; public class PoolController { ArrayList<Connection> conpool=new ArrayList<Connection>(); public int instancescount=0; int maxinstancescount=100; int ex=0; public PoolController() throws ClassNotFoundException{ Class.forName("org.sqlite.JDBC"); } public Connection add(){ Connection con=null; try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/lesson","root"); conpool.add(con); instancescount++; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } public Connection getconnection(){ Connection con=null; if(conpool.size()>0){ con=conpool.get(conpool.size()-1); conpool.remove(conpool.size()-1);}else { if(instancescount<=maxinstancescount){ ex++; //System.out.println(ex); con=add(); }else{ synchronized(conpool){ while(conpool.size()<=0){ try {conpool.wait();} catch (InterruptedException e) {} } } } } return con; } public void release(Connection con){ synchronized(conpool){ conpool.add(con); conpool.notify(); } } } 

I run several threads for simultaneous access to databases:

  package DataSourcepackage; import java.beans.PropertyVetoException; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import ObjectPool.PoolController; public class Mainthread extends Thread { DataSource ds; int count; PoolController poolcontroller; public Mainthread(PoolController poolcontroller,int count) throws IOException, SQLException, PropertyVetoException{ this.poolcontroller=poolcontroller; this.count=count; start(); } public void run(){ Connection con=poolcontroller.getconnection(); Statement stmt; try { stmt = con.createStatement(); String query="INSERT INTO lesson.tele2 VALUES ('asd',"+count+",'eq')"; stmt.executeUpdate(query); poolcontroller.release(con); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

this is the main class from which 500 threads start:

 package DataSourcepackage; import java.beans.PropertyVetoException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Calendar; import ObjectPool.PoolController; public class experiment { public static void main(String [] args) throws SQLException, ClassNotFoundException, IOException, PropertyVetoException{ PoolController pool=new PoolController(); long startTime = System.currentTimeMillis(); for(int i=0;i<500;i++){ new Mainthread(pool,i); } } } 

throws such an exception:

 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections" at sun.reflect.GeneratedConstructorAccessor33.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.Util.getInstance(Util.java:408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1040) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) at sun.reflect.GeneratedConstructorAccessor27.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at ObjectPool.PoolController.add(PoolController.java:20) at ObjectPool.PoolController.getconnection(PoolController.java:39) at DataSourcepackage.Mainthread.run(Mainthread.java:21) 
  • one
    This means that the server has a limit on the number of simultaneous connections (by default 151). Either remove / loosen this restriction, or revise the policy on using connections. Usually there is no need for such a huge number of connections that do not do anything. Connections are obtained from the pool for a relatively short time. Completed the work and as soon as possible returned back to the pool. However, you have a limiter in about 100 connections in the pool. It seems to me that they have incorrectly synchronized getconnection and release. - Sergey
  • Then I have a question to return to the pool to close the connection or leave the connection open and just shove back into the arraylist? It seems to me or the problem is not in the number of open connections, but in the number of compounds used? - user234553
  • When you return to the pool, the connection remains open. When an error occurred, look at the size of the array, compare it with the server parameter max_connections. I suspect that your connection retrieval functions are being completely incorrectly synchronized. Therefore, you get more than 100, which ultimately leads to the exhaustion of server resources. - Sergey
  • You have launched 500 threads. they got into getconnection at the same time and all of them, roughly speaking, instancescount == 0 . That is trying to open the program 500 connections. At 151 an error occurs. It is necessary to arrange synchronization more intelligently. If this is not a learning task, then use ready-made pools. Now even with the jdbc driver supplied. - Sergey
  • Yes, I already understood this, thanks for the tips. I just made the getconnection () function synchronized. - user234553

0