用于App服务端的MySQL连接池(支持高并发)
本文向大家介绍了简单的MySQL连接池,用于App服务端比较合适,分享给大家供大家参考,具体内容如下
/** *连接池类 */ packagecom.junones.test; importjava.sql.Connection; importjava.sql.SQLException; importjava.util.HashMap; importjava.util.Map; importjava.util.Map.Entry; importcom.mysql.jdbc.jdbc2.optional.MysqlDataSource; publicclassMySQLPool{ privatestaticvolatileMySQLPoolpool; privateMysqlDataSourceds; privateMap<Connection,Boolean>map; privateStringurl="jdbc:mysql://localhost:3306/test"; privateStringusername="root"; privateStringpassword="root1234"; privateintinitPoolSize=10; privateintmaxPoolSize=200; privateintwaitTime=100; privateMySQLPool(){ init(); } publicstaticMySQLPoolgetInstance(){ if(pool==null){ synchronized(MySQLPool.class){ if(pool==null){ pool=newMySQLPool(); } } } returnpool; } privatevoidinit(){ try{ ds=newMysqlDataSource(); ds.setUrl(url); ds.setUser(username); ds.setPassword(password); ds.setCacheCallableStmts(true); ds.setConnectTimeout(1000); ds.setLoginTimeout(2000); ds.setUseUnicode(true); ds.setEncoding("UTF-8"); ds.setZeroDateTimeBehavior("convertToNull"); ds.setMaxReconnects(5); ds.setAutoReconnect(true); map=newHashMap<Connection,Boolean>(); for(inti=0;i<initPoolSize;i++){ map.put(getNewConnection(),true); } }catch(Exceptione){ e.printStackTrace(); } } publicConnectiongetNewConnection(){ try{ returnds.getConnection(); }catch(SQLExceptione){ e.printStackTrace(); } returnnull; } publicsynchronizedConnectiongetConnection(){ Connectionconn=null; try{ for(Entry<Connection,Boolean>entry:map.entrySet()){ if(entry.getValue()){ conn=entry.getKey(); map.put(conn,false); break; } } if(conn==null){ if(map.size()<maxPoolSize){ conn=getNewConnection(); map.put(conn,false); }else{ wait(waitTime); conn=getConnection(); } } }catch(Exceptione){ e.printStackTrace(); } returnconn; } publicvoidreleaseConnection(Connectionconn){ if(conn==null){ return; } try{ if(map.containsKey(conn)){ if(conn.isClosed()){ map.remove(conn); }else{ if(!conn.getAutoCommit()){ conn.setAutoCommit(true); } map.put(conn,true); } }else{ conn.close(); } }catch(SQLExceptione){ e.printStackTrace(); } } } /** *测试类 */ packagecom.junones.test; importjava.sql.Connection; importjava.sql.ResultSet; importjava.sql.SQLException; importjava.sql.Statement; publicclassTestMySQLPool{ privatestaticvolatileinta; privatesynchronizedstaticvoidincr(){ a++; } publicstaticvoidmain(String[]args)throwsInterruptedException{ inttimes=10000; longstart=System.currentTimeMillis(); for(inti=0;i<times;i++){ newThread(newRunnable(){ @Override publicvoidrun(){ MySQLPoolpool=MySQLPool.getInstance(); Connectionconn=pool.getConnection(); Statementstmt=null; ResultSetrs=null; try{ stmt=conn.createStatement(); rs=stmt.executeQuery("selectid,namefromt_test"); while(rs.next()){ System.out.println(rs.getInt(1)+"," +rs.getString(2)); } }catch(SQLExceptione){ e.printStackTrace(); }finally{ incr(); if(rs!=null){ try{ rs.close(); }catch(SQLExceptione){ e.printStackTrace(); } } if(stmt!=null){ try{ stmt.close(); }catch(SQLExceptione){ } } pool.releaseConnection(conn); } } }).start(); } while(true){ if(a==times){ System.out.println("finished,time:" +(System.currentTimeMillis()-start)); break; } Thread.sleep(100); } } }
测试结果:1万个并发,5秒完成。
以上就是为大家分享的MySQL连接池类,希望大家喜欢,谢谢大家的关注。