⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 namedconnectionpool.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         if (this.eraseUnusedPoolTimeout == -1)            this.eraseUnusedPoolTimeout = glob.getProperty().get("JdbcPool.eraseUnusedPoolTimeout", 60*60*1000L); // If a user disappears for one hour, delete his pool         if (this.eraseUnusedPoolTimeout > 0 && this.eraseUnusedPoolTimeout < 1000L)            this.eraseUnusedPoolTimeout = 1000L; // minimum         if (maxInstances == -1)            maxInstances = glob.getProperty().get("JdbcPool.maxInstances", 20); // Max. number of connections         if (busyToIdle == -1)            busyToIdle = glob.getProperty().get("JdbcPool.busyToIdleTimeout", 0); // How long may a query last         if (idleToErase == -1)            idleToErase = glob.getProperty().get("JdbcPool.idleToEraseTimeout", 10*60*1000L); // How long does an unused connection survive (10 min)         this.maxResourceExhaustRetries = glob.getProperty().get("JdbcPool.maxResourceExhaustRetries", 5);         resourceExhaustSleepGap = glob.getProperty().get("JdbcPool.resourceExhaustSleepGap", 1000);   // milli         poolManager = new PoolManager(ME, this, maxInstances, busyToIdle, idleToErase);         if (this.eraseUnusedPoolTimeout >= 1000L)            synchronized(this.timeoutMonitor) {               this.timeoutHandle = this.poolManager.getTransistionTimer().addTimeoutListener(this, this.eraseUnusedPoolTimeout, "dummy");            }      }      /** This callback does nothing (enforced by interface I_PoolManager) */      public void idleToBusy(Object resource) {         // Connection con = (Connection)resource;      }      /** This callback does nothing (enforced by interface I_PoolManager */      public void busyToIdle(Object resource) {         // Connection con = (Connection)resource;      }      /**       * Create a new JDBC connection, the driver must be registered already.       */      public Object toCreate(String instanceId) throws XmlBlasterException {         try {            return DriverManager.getConnection (dbUrl, dbUser, dbPasswd);         }         catch(Exception e) {            //log.error(ME, "System Exception in connect(" + dbUrl + ", " + dbUser + "): " + e.toString());            throw new XmlBlasterException(glob, ErrorCode.RESOURCE_DB_UNAVAILABLE, ME, "Couldn't open database connection dbUrl=" + dbUrl + " dbUser=" + dbUser + ": " + e.toString());         }      }      /**       * Destroy the JDBC connection.       * The driver remains.       * @param The Connection object       */      public void toErased(Object resource) {         Connection con = (Connection)resource;         try {            con.close();            if (log.isLoggable(Level.FINE)) log.fine("JDBC connection closed for '" + dbUrl + "', '" + dbUser + "'");         }         catch (Exception e) {            log.severe("System Exception in close JDBC connection: " + e.toString());            // For example Oracle throws this if you have shutdown Oracle in the mean time:            // System Exception in close JDBC connection: java.sql.SQLException: Io exception: End of TNS data channel         }      }      /**       * Use this method to get a JDBC connection.       */      Connection reserve() throws XmlBlasterException {         if (poolManager == null) { throw new XmlBlasterException(glob, ErrorCode.INTERNAL_ILLEGALSTATE, ME+".Destroyed", "Pool is destroyed"); }         if (log.isLoggable(Level.FINE)) log.fine("Entering reserve '" + dbUrl + "', '" + dbUser + "'");         int ii=0;         while (true) {            try {               synchronized(meetingPoint) {                  if (this.eraseUnusedPoolTimeout > 200L) {                     synchronized(this.timeoutMonitor) {                        this.timeoutHandle = this.poolManager.getTransistionTimer().refreshTimeoutListener(this.timeoutHandle, this.eraseUnusedPoolTimeout);                     }                  }                  ResourceWrapper rw = poolManager.reserve(PoolManager.USE_OBJECT_REF);                  Connection con = (Connection)rw.getResource();                  return con;               }            }            catch (XmlBlasterException e) {               // id.equals("ResourceExhaust")               if (e.getErrorCode() == ErrorCode.RESOURCE_EXHAUST && ii < this.maxResourceExhaustRetries) {                  if (ii == 0) log.warning("Caught exception in reserve(), going to poll " + this.maxResourceExhaustRetries + " times every " + resourceExhaustSleepGap + " millis");                  Timestamp.sleep(resourceExhaustSleepGap);                  ii++;               }               else {                  //log.error(ME, "Caught exception in reserve(): " + e.toString() + "\n" + toXml());                  throw e;               }            }         }      }      /**       * Use this method to release a JDBC connection.       */      void release(Connection con) throws XmlBlasterException {         if (poolManager == null) { throw new XmlBlasterException(glob, ErrorCode.INTERNAL_ILLEGALSTATE, ME+".Destroyed", "Pool is destroyed"); }         if (log.isLoggable(Level.FINE)) log.fine("Entering release '" + dbUrl + "', '" + dbUser + "' conId=" + con);         try {            synchronized(meetingPoint) {               poolManager.release(""+con);            }         }         catch (XmlBlasterException e) {            Thread.dumpStack();            log.severe("Caught exception in release(): " + e.toString());            throw e;         }      }      /**       * Destroy a JDBC connection (from busy or idle to undef).        */      void erase(Connection con) throws XmlBlasterException {         if (poolManager == null) { throw new XmlBlasterException(glob, ErrorCode.INTERNAL_ILLEGALSTATE, ME+".Destroyed", "Pool is destroyed"); }         if (log.isLoggable(Level.FINE)) log.fine("Entering erase '" + dbUrl + "', '" + dbUser + "' conId=" + con);         synchronized(meetingPoint) {            poolManager.erase(""+con);         }      }      /**       * Timeout callback enforced by I_Timeout.        * Destroys pool of a user with all its connections       */      public void timeout(java.lang.Object o)      {         if (log.isLoggable(Level.FINE)) log.fine("Entering pool destroy timeout for '" + dbUrl + "', '" + dbUser + "' ...");         synchronized(meetingPoint) {            if (poolManager.getNumBusy() != 0) {               log.warning("Can't destroy pool from '" + dbUrl + "', '" + dbUser + "', he seems to be busy working on his database.");               if (this.eraseUnusedPoolTimeout > 200L) {                  synchronized(this.timeoutMonitor) {                     this.timeoutHandle = this.poolManager.getTransistionTimer().addTimeoutListener(this, this.eraseUnusedPoolTimeout, "dummy");                     //[UnnamedConnectionPool] Can't destroy pool from 'jdbc:oracle:thin:@localhost:1521:xmlb', 'joe', he seems to be busy working on his database.                  }               }               return;            }            try {               boss.destroy(dbUrl, dbUser, dbPasswd);            } catch(XmlBlasterException e) {               log.severe("timeout: " + e.toString());            }         }      }      /** Destroy the complete unnamed pool */      void destroy() {         synchronized(meetingPoint) {            if (poolManager != null) {               if (log.isLoggable(Level.FINE)) log.fine("Destroying pool: " + poolManager.toXml());               poolManager.destroy();               poolManager = null;            }         }         dbUrl = null;         dbUser = null;      }      /**       * Dump state of this object into a XML ASCII string.       */      public final String toXml()      {         StringBuffer buf = new StringBuffer(256);         String offset = "\n   ";         buf.append(offset).append("<").append(ME).append(" url='").append(dbUrl).append("' user='").append(dbUser).append("' this.eraseUnusedPoolTimeout='").append(this.eraseUnusedPoolTimeout).append("'>");         if (poolManager != null)            buf.append(poolManager.toXml("      "));         else            buf.append(poolManager.toXml("      DESTROYED"));         buf.append(offset).append("</" + ME + ">");         return buf.toString();      }   }   /**    * For testing only.    * <p />    * Invoke: java org.xmlBlaster.protocol.jdbc.NamedConnectionPool -logging FINE    */   public static void main(String[] args) {      /*      NamedConnectionPool namedPool = null;      try {         log.setLogLevel(args); // initialize log level         String ME = "TestConnection";         String dbDriver = Args.getArg(args, "-dbDriver", "oracle.jdbc.driver.OracleDriver");         final String dbUrl = Args.getArg(args, "-dbUrl", "jdbc:oracle:thin:@localhost:1521:MARCEL");         final String dbUser = Args.getArg(args, "-dbUser", "mrf");         final String dbPasswd = Args.getArg(args, "-dbPasswd", "mrf");         final String dbUser2 = Args.getArg(args, "-dbUser2", "system");         final String dbPasswd2 = Args.getArg(args, "-dbPasswd2", "manager");         final String dbStmt = Args.getArg(args, "-dbStmt", "select * from user_tables");         Class cl = Class.forName(dbDriver);         java.sql.Driver dr = (java.sql.Driver)cl.newInstance();         java.sql.DriverManager.registerDriver(dr);         log.info(ME, "Jdbc driver '" + dbDriver + "' loaded.");         namedPool = new NamedConnectionPool();         final long timeToDeath = 10*1000L;         class Test extends Thread {            private String ME = "TestThread";            private NamedConnectionPool np;            private String user;            private String pw;            Test(String name, NamedConnectionPool namedP, String user, String pw) {               super(name);               this.np = namedP;               this.ME = name;               this.user = user;               this.pw = pw;            }            public void run() {               try {                  for (int ii=0; ii<50; ii++) {                     log.info(ME, " query run=" + ii + "\n");                     org.xmlBlaster.util.StopWatch watch = new org.xmlBlaster.util.StopWatch();                     Connection con = np.reserve(dbUrl, user, pw, timeToDeath, 100, 40*1000L);                     log.info(ME, "Reserved connection id=" + con + watch.toString());                     //log.info(ME, np.toXml());                     java.sql.Statement stmt = null;                     java.sql.ResultSet rs = null;                     try {                        stmt = con.createStatement();                        rs = stmt.executeQuery(dbStmt);                     } finally {                        if (rs!=null) rs.close();                        if (stmt!=null) stmt.close();                        watch = new org.xmlBlaster.util.StopWatch();                        if (con!=null) np.release(dbUrl, user, pw, con);                        log.info(ME, "Query successful done, connection released" + watch.toString());                        //log.info(ME, np.toXml());                     }                  }                  log.info(ME, "Going to sleep " + (timeToDeath+1000L) + " msec");                  try { Thread.currentThread().sleep(timeToDeath+1000L); } catch( InterruptedException i) {}                  log.info(ME, "After sleeping " + (timeToDeath+1000L) + " msec, erased connection\n" + np.toXml());               }               catch(Throwable e) {                  e.printStackTrace();                  if (np!=null) { np.destroy(); np = null; } // this error handling is not thread save                  log.panic(ME, "TEST FAILED" + e.toString());               }            }         }         java.util.Vector vec = new java.util.Vector();         for (int ii=0; ii<8; ii++) {            String name = "TestThread-"+ii;            if (true) { //(ii % 2) == 0) {               Test p = new Test(name, namedPool, dbUser, dbPasswd);               p.setDaemon(true);               p.start();               vec.addElement(p);            }            else {               Test p = new Test(name, namedPool, dbUser2, dbPasswd2);               p.setDaemon(true);               p.start();               vec.addElement(p);            }            log.info(ME, "Started " + name + " ...");         }         for (int ii=0; ii<vec.size(); ii++) {            Test p = (Test)vec.elementAt(ii);            p.join();         }         log.info(ME, "All done, destroying ...");         namedPool.destroy();      } catch (Throwable e) {         namedPool.destroy();         log.panic(ME, "ERROR: Test failed " + e.toString());      }      */   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -