📄 connectionpool.java
字号:
*/ public void run() { while (true) { // print warnings on connections for (int i = 0; i < maxCon; i++) { if (cons[i] == null) { continue; } try { SQLWarning warning = cons[i].getWarnings(); if (warning != null) { Log.warn("Connection " + i + " had warnings: " + warning); cons[i].clearWarnings(); } } catch (SQLException e) { Log.warn("Unable to get warning for connection: ", e); } } int lastOpen = -1; // go over every connection, check it's health for (int i = maxCon - 1; i >= 0; i--) { if (wrappers[i] == null) { continue; } try { long time = System.currentTimeMillis(); synchronized (wrappers[i]) { if (wrappers[i].checkedout) { if (lastOpen < i) { lastOpen = i; } // if the jive property "database.defaultProvider.checkOpenConnections" // is true check open connections to make sure they haven't been open // for more than XX seconds (600 by default) if ("true".equals(JiveGlobals.getXMLProperty("database.defaultProvider.checkOpenConnections")) && !wrappers[i].hasLoggedException) { int timeout = 600; try { timeout = Integer.parseInt(JiveGlobals.getXMLProperty("database.defaultProvider.openConnectionTimeLimit")); } catch (Exception e) { /* ignore */ } if (time - wrappers[i].lockTime > timeout * 1000) { wrappers[i].hasLoggedException = true; Log.warn("Connection has been held open for too long: ", wrappers[i].exception); } } continue; } wrappers[i].checkedout = true; } // test health of connection Statement stmt = null; try { stmt = cons[i].createStatement(); } finally { if (stmt != null) { stmt.close(); } } // Can never tell if (cons[i].isClosed()) { throw new SQLException(); } // check the age of the connection if (time - wrappers[i].createTime > conTimeout) { throw new SQLException(); } // check to see if it's the last connection and if it's been idle for // more than 60 secounds if ((time - wrappers[i].checkinTime > 60 * 1000) && i > minCon && lastOpen <= i) { synchronized (conCountLock) { cons[i].close(); wrappers[i] = null; cons[i] = null; conCount--; } } // Flag the last open connection lastOpen = i; // Unlock the connection if (wrappers[i] != null) { wrappers[i].checkedout = false; } } catch (SQLException e) { try { synchronized (conCountLock) { cons[i].close(); wrappers[i] = createCon(i); // unlock the connection wrappers[i].checkedout = false; } } catch (SQLException sqle) { Log.warn("Failed to reopen connection", sqle); synchronized (conCountLock) { wrappers[i] = null; cons[i] = null; conCount--; } } } } try { Thread.sleep(30 * 1000); } catch (InterruptedException e) { return; } } } private synchronized ConnectionWrapper getCon() throws SQLException { // check to see if there's a connection already available for (int i = 0; i < conCount; i++) { ConnectionWrapper wrapper = wrappers[i]; // null means that the connection hasn't been initialized, which will only occur // if the current index is greater than the current connection count if (wrapper == null) { break; } synchronized (wrapper) { if (!wrapper.checkedout) { wrapper.setConnection(cons[i]); wrapper.checkedout = true; wrapper.lockTime = System.currentTimeMillis(); if ("true".equals(JiveGlobals.getXMLProperty("database.defaultProvider.checkOpenConnections"))) { wrapper.exception = new Exception(); wrapper.hasLoggedException = false; } return wrapper; } } } // won't create more than maxConnections synchronized (conCountLock) { if (conCount >= maxCon) { return null; } ConnectionWrapper con = createCon(conCount); conCount++; return con; } } /** * Create a connection, wrap it and add it to the array of open wrappers */ private ConnectionWrapper createCon(int index) throws SQLException { try { Connection con = null; ClassUtils.forName(driver); if (mysqlUseUnicode) { Properties props = new Properties(); props.put("characterEncoding", "UTF-8"); props.put("useUnicode", "true"); if (username != null) { props.put("user", username); } if (password != null) { props.put("password", password); } con = DriverManager.getConnection(serverURL, props); } else { con = DriverManager.getConnection(serverURL, username, password); } if (con == null) { throw new SQLException("Unable to retrieve connection from DriverManager"); } try { con.setAutoCommit(true); } catch (SQLException e) {/* ignored */ } // A few people have been having problems because the default transaction // isolation level on databases is too high. READ_COMMITTED is a good // value for everyone to use because it provides the minimum amount of // locking that Jive needs to work well. try { // Supports transactions? if (con.getMetaData().supportsTransactions()) { con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } } catch (SQLException e) { // Ignore errors. A few databases don't support setting the transaction // isolation level, but ignoring the error shouldn't cause problems. } // create the wrapper object and mark it as checked out ConnectionWrapper wrapper = new ConnectionWrapper(con, this); if ("true".equals(JiveGlobals.getXMLProperty("database.defaultProvider.checkOpenConnections"))) { wrapper.exception = new Exception(); } synchronized (conCountLock) { cons[index] = con; wrappers[index] = wrapper; } return wrapper; } catch (ClassNotFoundException e) { Log.error(e); throw new SQLException(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -