java jdbc 连接池_用java实现JDBC数据库连接池

1 packagecn.com.css.cas.jdbc;2

3 importjava.sql.Connection;4 importjava.sql.Driver;5 importjava.sql.DriverManager;6 importjava.sql.SQLException;7 importjava.util.HashSet;8 importjava.util.Iterator;9 importjava.util.LinkedList;10

11 /**

12 * JDBC数据库连接池13 *14 *@authorWoud15 *16 */

17 public classSimpleConnetionPool {18 private static LinkedList m_notUsedConnection = newLinkedList();19 private static HashSet m_usedUsedConnection = newHashSet();20 private static String m_url = "";21 private static String m_user = "";22 private static String m_password = "";23 private static int m_maxConnect = 3;24 static final boolean DEBUG = false;25 static private long m_lastClearClosedConnection =System26 .currentTimeMillis();27 public static long CHECK_CLOSED_CONNECTION_TIME = 5000; //5秒

28

29 static{30 try{31 initDriver();32 } catch(InstantiationException e) {33 //TODO Auto-generated catch block

34 e.printStackTrace();35 } catch(IllegalAccessException e) {36 //TODO Auto-generated catch block

37 e.printStackTrace();38 } catch(ClassNotFoundException e) {39 //TODO Auto-generated catch block

40 e.printStackTrace();41 }42 }43

44 publicSimpleConnetionPool(String url, String user, String password) {45 m_url =url;46 m_user =user;47 m_password =password;48 }49

50 private static void initDriver() throwsInstantiationException,51 IllegalAccessException, ClassNotFoundException {52 Driver driver = null;53

54 //读取MySql的Driver

55 driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();56 installDriver(driver);57

58 /*

59 * // 读取postgresql的driver driver = (Driver)60 * Class.forName("org.postgresql.Driver").newInstance();61 * installDriver(driver);62 */

63

64 }65

66 public static voidinstallDriver(Driver driver) {67 try{68 DriverManager.registerDriver(driver);69 } catch(SQLException e) {70 //TODO Auto-generated catch block

71 e.printStackTrace();72 }73 }74

75 public static synchronizedConnection getConnection() {76 //关闭清除多余的连接

77 clearClosedConnection();78

79 //输出当前总连接数

80 if(DEBUG)81 System.out.println("当前总连接数:" +getConnectionCount());82

83 //寻找空闲的连接

84 while (m_notUsedConnection.size() > 0) {85 try{86 Connection con =(Connection) m_notUsedConnection.removeFirst();87

88 if(con.isClosed()) {89 continue;90 }91

92 m_usedUsedConnection.add(con);93 if(DEBUG) {94 //System.out.println("连接初始化成功");

95 }96 returncon;97 } catch(SQLException e) {98 }99 }100

101 //没有找到,建立一些新的连接以供使用

102 int newCount =getIncreasingConnectionCount();103 LinkedList list = newLinkedList();104 Connection con = null;105

106 for (int i = 0; i < newCount; i++) {107 con =getNewConnection();108 if (con != null) {109 list.add(con);110 }111 }112

113 //没有成功建立连接,访问失败

114 if (list.size() == 0)115 return null;116

117 //成功建立连接,使用的加入used队列,剩下的加入notUsed队列

118 con =(Connection) list.removeFirst();119 m_usedUsedConnection.add(con);120 m_notUsedConnection.addAll(list);121 list.clear();122

123 returncon;124 }125

126 public staticConnection getNewConnection() {127 try{128 Connection con =DriverManager.getConnection(m_url, m_user,129 m_password);130 returncon;131 } catch(SQLException e) {132 //TODO Auto-generated catch block

133 e.printStackTrace();134 }135

136 return null;137 }138

139 static synchronized voidpushConnectionBackToPool(Connection con) {140 boolean exist =m_usedUsedConnection.remove(con);141 if(exist) {142 m_notUsedConnection.addLast(con);143 }144 }145

146 public static intclose() {147 int count = 0;148

149 Iterator iterator =m_notUsedConnection.iterator();150 while(iterator.hasNext()) {151 try{152 ((Connection) iterator.next()).close();153 count++;154 } catch(SQLException e) {155 //TODO Auto-generated catch block

156 e.printStackTrace();157 }158 }159 m_notUsedConnection.clear();160

161 iterator =m_usedUsedConnection.iterator();162 while(iterator.hasNext()) {163 try{164 ((Connection) iterator.next()).close();165 } catch(SQLException e) {166 //TODO Auto-generated catch block

167 e.printStackTrace();168 }169 }170 m_usedUsedConnection.clear();171

172 returncount;173 }174

175 private static voidclearClosedConnection() {176 long time =System.currentTimeMillis();177

178 //时间不合理,没有必要检查

179 if (time

184 //时间太短,没有必要检查

185 if (time - m_lastClearClosedConnection

189 m_lastClearClosedConnection =time;190

191 //开始检查没有使用的Connection

192 Iterator iterator =m_notUsedConnection.iterator();193 while(iterator.hasNext()) {194 Connection con =(Connection) iterator.next();195

196 try{197 if(con.isClosed()) {198 iterator.remove();199 }200 } catch(SQLException e) {201 iterator.remove();202

203 if(DEBUG) {204 System.out.println("问题连接已断开");205 }206 }207 }208

209 //清除多余的Connection

210 int decrease =getDecreasingConnectionCount();211

212 while (decrease > 0 && m_notUsedConnection.size() > 0) {213 Connection con =(Connection) m_notUsedConnection.removeFirst();214

215 try{216 con.close();217 } catch(SQLException e) {218

219 }220

221 decrease--;222 }223 }224

225 public static intgetIncreasingConnectionCount() {226 int count = 1;227 count = getConnectionCount() / 4;228

229 if (count < 1)230 count = 1;231

232 returncount;233 }234

235 public static intgetDecreasingConnectionCount() {236 int count = 0;237

238 if (getConnectionCount() >m_maxConnect) {239 count = getConnectionCount() -m_maxConnect;240 }241

242 returncount;243 }244

245 public static synchronized intgetNotUsedConnectionCount() {246 returnm_notUsedConnection.size();247 }248

249 public static synchronized intgetUsedConnectionCount() {250 returnm_usedUsedConnection.size();251 }252

253 public static synchronized intgetConnectionCount() {254 return m_notUsedConnection.size() +m_usedUsedConnection.size();255 }256

257 }

你可能感兴趣的:(java,jdbc,连接池)