daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest Jan 20th, 2017 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ObjectPool;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.util.ArrayList;
  6. public class PoolController {
  7.  
  8. ArrayList<Connection> conpool=new ArrayList<Connection>();
  9. public int instancescount=0;
  10. int maxinstancescount=100;
  11. int ex=0;
  12. public PoolController() throws ClassNotFoundException{
  13. Class.forName("org.sqlite.JDBC");
  14. }
  15. public Connection add(){
  16. Connection con=null;
  17. try {
  18. con=DriverManager.getConnection("jdbc:mysql://localhost:3306/lesson","root");
  19.     conpool.add(con);
  20.     instancescount++;
  21. } catch (SQLException e) {
  22.     // TODO Auto-generated catch block
  23.     e.printStackTrace();
  24. }
  25.  
  26. return con;
  27. }  
  28. public Connection getconnection(){
  29. Connection con=null;
  30.  
  31. if(conpool.size()>0){
  32.  con=conpool.get(conpool.size()-1);
  33. conpool.remove(conpool.size()-1);}else {
  34.     if(instancescount<=maxinstancescount){
  35.         ex++;
  36.         //System.out.println(ex);
  37.         con=add();
  38.     }else{
  39.     synchronized(conpool){  
  40. while(conpool.size()<=0){
  41.  
  42.     try {conpool.wait();} catch (InterruptedException e) {}
  43. }
  44.  
  45. }      
  46.  
  47.     }  
  48. }
  49.  
  50.     return con;
  51. }
  52.  
  53. public void release(Connection con){
  54. synchronized(conpool){
  55.     conpool.add(con);
  56.     conpool.notify();
  57. }
  58.  
  59. }
  60.  
  61.  }
  62.    
  63. package DataSourcepackage;
  64.  
  65.    import java.beans.PropertyVetoException;
  66.    import java.io.IOException;
  67.    import java.sql.Connection;
  68.    import java.sql.SQLException;
  69.    import java.sql.Statement;
  70.    import ObjectPool.PoolController;
  71.  
  72.    public class Mainthread extends Thread {
  73.    DataSource ds;
  74.    int count;
  75.    PoolController poolcontroller;
  76.    public Mainthread(PoolController poolcontroller,int count) throws  IOException, SQLException, PropertyVetoException{
  77.    this.poolcontroller=poolcontroller;
  78.    this.count=count;
  79.    start();
  80.    }
  81.    public void run(){
  82.     Connection con=poolcontroller.getconnection();
  83.     Statement stmt;
  84.     try {
  85.  
  86.         stmt = con.createStatement();
  87.         String query="INSERT INTO lesson.tele2   VALUES ('asd',"+count+",'eq')";
  88.         stmt.executeUpdate(query);
  89.         poolcontroller.release(con);
  90.  
  91.        } catch (SQLException e) {
  92.         // TODO Auto-generated catch block
  93.         e.printStackTrace();
  94.         }
  95.  
  96.  
  97.  
  98.         }
  99.  
  100.          }
  101.    
  102. package DataSourcepackage;
  103.  
  104. import java.beans.PropertyVetoException;
  105. import java.io.IOException;
  106. import java.sql.Connection;
  107. import java.sql.DriverManager;
  108. import java.sql.ResultSet;
  109. import java.sql.SQLException;
  110. import java.sql.Statement;
  111. import java.util.Calendar;
  112.  
  113. import ObjectPool.PoolController;
  114.  
  115. public class experiment {
  116. public static void main(String [] args) throws SQLException,  ClassNotFoundException, IOException, PropertyVetoException{
  117.  
  118.  
  119.     PoolController pool=new PoolController();
  120.     long startTime = System.currentTimeMillis();
  121.     for(int i=0;i<500;i++){
  122.         new Mainthread(pool,i);
  123.     }
  124.  
  125.  
  126.  
  127.      }
  128.      }
  129.    
  130. com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data   source rejected establishment of connection,  message from server: "Too many  connections"
  131.   at sun.reflect.GeneratedConstructorAccessor33.newInstance(Unknown Source)
  132.   at   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  133. at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  134. at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
  135. at com.mysql.jdbc.Util.getInstance(Util.java:408)
  136. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
  137. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
  138. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
  139. at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1040)
  140. at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
  141. at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
  142. at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
  143. at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
  144. at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
  145. at sun.reflect.GeneratedConstructorAccessor27.newInstance(Unknown Source)
  146. at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  147. at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  148. at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
  149. at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
  150. at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
  151. at java.sql.DriverManager.getConnection(DriverManager.java:664)
  152. at java.sql.DriverManager.getConnection(DriverManager.java:247)
  153. at ObjectPool.PoolController.add(PoolController.java:20)
  154. at ObjectPool.PoolController.getconnection(PoolController.java:39)
  155. at DataSourcepackage.Mainthread.run(Mainthread.java:21)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top