📄 jdbcconnectionpool.java
字号:
if (this.status != I_StorageProblemListener.UNAVAILABLE) { int oldStatus = this.status; synchronized (this) { if (this.status == I_StorageProblemListener.UNAVAILABLE) return; oldStatus = this.status; if (this.status != I_StorageProblemListener.UNAVAILABLE) { this.status = I_StorageProblemListener.UNAVAILABLE; this.waitingForReentrantConnections = true; } } // start polling to wait until all connections have returned // start pooling to see if new connections can be established again this.glob.getJdbcConnectionPoolTimer().addTimeoutListener(this, this.reconnectionTimeout, null); I_StorageProblemListener lst = this.storageProblemListener; lst.storageUnavailable(oldStatus); } } /** * Returns a free connection. If no connection is currently available * it waits no more than what specified in the configuration. If after that time * still no connection is available it throws an XmlBlasterException. It also throws * an XmlBlasterException if the number of threads which are waiting is already * too high (also configurable). */ public Connection getConnection() throws XmlBlasterException { if (this.status != I_StorageProblemListener.AVAILABLE) throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_DB_UNAVAILABLE, ME, "getConnection: Connection Lost. Going in polling modus"); if (this.waitingCalls >= this.maxWaitingThreads) throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_TOO_MANY_THREADS, ME, "Too many threads waiting for a connection to the DB. Increase the property 'queue.persistent.maxWaitingThreads'"); synchronized (this) { this.waitingCalls++; } if (log.isLoggable(Level.FINER)) log.finer("getConnection " + this.connections.size() + " waiting calls: " + this.waitingCalls); try { if (this.isShutdown) connect(false, false); return get(this.connectionBusyTimeout); } catch (SQLException ex) { String additionalMsg = "check system classpath and 'jdbc.drivers' system property\n"; additionalMsg += "'classpath' is: '" + System.getProperty("classpath", "") + "'\n"; additionalMsg += "'jdbc.drivers' is: '" + System.getProperty("jdbc.drivers", "") + "'\n"; throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_DB_UNAVAILABLE, ME + ".getConnection()", ex.getMessage() + " " + additionalMsg); } finally { synchronized (this) { this.waitingCalls--; } } } /** * Used to give back a connection to the pool. If the pool is already full * it will throw an exception. If the success flag is true, it well release the * connection back to the pool, otherwise it will throw away the current connection * and add to the pool a new one. */ public void releaseConnection(Connection conn, boolean success) throws XmlBlasterException { if (success) releaseConnection(conn); else discardConnection(conn); } /** * Used to give back a connection to the pool. If the pool is already full * it will throw an exception. */ private void releaseConnection(Connection conn) throws XmlBlasterException { if (log.isLoggable(Level.FINER)) log.finer("releaseConnection " + this.connections.size() + " waiting calls: " + this.waitingCalls); try { SQLWarning warns = conn.getWarnings(); /* while (warns != null) { log.warn(ME, "errorCode=" + warns.getErrorCode() + " state=" + warns.getSQLState() + ": " + warns.toString().trim()); Thread.currentThread().dumpStack(); warns = warns.getNextWarning(); } */ if (log.isLoggable(Level.FINE)) { while (warns != null) { log.fine("errorCode=" + warns.getErrorCode() + " state=" + warns.getSQLState() + ": " + warns.toString().trim()); warns = warns.getNextWarning(); } } conn.clearWarnings(); // free memory } catch (Throwable e) { log.warning("clearWarnings() failed: " + e.toString()); } boolean isOk = put(conn); // if an exception occured it would be better to throw away the connection and make a new one if (!isOk) { log.severe("the connection pool is already full: " + ThreadLister.listAllThreads()); } } public void dumpMetaData() { Connection conn = null; try { if (this.isShutdown) connect(false, false); conn = getConnection(); DatabaseMetaData metaData = conn.getMetaData(); log.info("--------------- DUMP OF METADATA FOR THE DB START---------------------"); String driverName = metaData.getDriverName(); String productName = metaData.getDatabaseProductName(); log.info("Driver name :'" + driverName +"', product name: '" + productName + "'"); log.info("max binary length : " + metaData.getMaxBinaryLiteralLength()); log.info("max char lit. length : " + metaData.getMaxCharLiteralLength()); log.info("max column length : " + metaData.getMaxColumnNameLength()); log.info("max cols. in table : " + metaData.getMaxColumnsInTable()); log.info("max connections : " + metaData.getMaxConnections()); log.info("max statement length : " + metaData.getMaxStatementLength()); log.info("max nr. of statements: " + metaData.getMaxStatements()); log.info("max tablename length : " + metaData.getMaxTableNameLength()); log.info("url : " + metaData.getURL()); log.info("support for trans. : " + metaData.supportsTransactions()); log.info("support transactions : " + getIsolationLevel(conn)); log.info("--------------- DUMP OF METADATA FOR THE DB END ---------------------"); } catch (XmlBlasterException ex) { log.severe("dumpMetaData: exception: " + ex.getMessage()); ex.printStackTrace(); } catch (SQLException ex) { log.severe("dumpMetaData: SQL exception: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (conn != null) releaseConnection(conn); } catch (XmlBlasterException ex) { log.severe("dumpMetaData: releasing the connection: " + ex.getMessage()); ex.printStackTrace(); } } } public boolean isDbAdmin() { return this.dbAdmin; } public int getQueryTimeout() { return this.queryTimeout; } public static void main(String[] args) { //here starts the main method ... Connection conn = null; try { Global glob = Global.instance(); glob.init(args); log.info("starting the application: The Tests start here "); String prefix = "cb.queue.persistent"; org.xmlBlaster.util.property.Property prop = glob.getProperty(); String xmlBlasterJdbc = prop.get("JdbcDriver.drivers", "org.postgresql.Driver"); System.setProperty("jdbc.drivers", xmlBlasterJdbc); String url = prop.get(prefix + ".url", ""); String user = prop.get(prefix + ".user", "postgres"); String password = prop.get(prefix + ".password", ""); if (log.isLoggable(Level.FINEST)) { log.finest("initialize -prefix is : " + prefix); log.finest("initialize -url : " + url); log.finest("initialize -user : " + user); log.finest("initialize -password : " + password); log.finest("initialize -driver list : " + xmlBlasterJdbc); } log.info("establishing the connection"); conn = DriverManager.getConnection(url, user, password); log.info("connection established. Sleeping 1 second"); Thread.sleep(1000L); log.info("finished"); } catch (/*SQL*/Exception ex) { if (log !=null) log.severe(" connecting to DB, error code: " + " : " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (log != null) log.info("closing the connection"); if (conn != null) { conn.close(); } if (log != null) log.info("connection closed"); } catch (Exception ex) { ex.printStackTrace(); } } } public synchronized void shutdown() { if (log.isLoggable(Level.FINER)) log.finer("shutdown"); disconnect();// this.initialized = false; this.isShutdown = true; } synchronized public void registerManager(JdbcManagerCommonTable manager) { if (log.isLoggable(Level.FINER)) log.finer("registerManager, number of managers registered (before registering this one): '" + this.managerCount + "'"); if (manager == null) return; this.managerCount++; } synchronized public void unregisterManager(JdbcManagerCommonTable manager) { if (log.isLoggable(Level.FINER)) log.finer("unregisterManager, number of managers registered (still including this one): '" + this.managerCount + "'"); if (manager == null) return; this.managerCount--; if (this.managerCount == 0) shutdown(); } /** * The batch mode means that insertions in the database are made in batch mode, * i.e. several entries in one sweep. This can increase perfomance significantly * on some DBs. * * @return true if batch mode has been enabled, false otherwise (defaults to true). */ public boolean isBatchModeEnabled() { return this.enableBatchMode; } /** * * @return true if 'cascadeDeleteSupported' has been set to true */ public boolean isCascadeDeleteSuppported() { return this.cascadeDeleteSupported; } /** * * @return true if 'nestedBracketsSupported' has been set to true */ public boolean isNestedBracketsSuppported() { return this.nestedBracketsSupported; } /** * @return the url */ public String getUrl() { return (this.url==null) ? "" : this.url; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -