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

📄 dbpool.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * Create a new JDBC connection, the driver must be registered already.   * @param instanceId A unique identifier   * @return The JDBC connection   */   public Object toCreate(String instanceId) {      try {         return DriverManager.getConnection (this.dbUrl, this.dbUser, this.dbPasswd);      }      catch(Exception e) {         throw new IllegalArgumentException(this.getClass().getName() + ": Couldn't open database connection dbUrl=" + this.dbUrl + " dbUser=" + this.dbUser + ": " + e.toString());      }   }   /**   * Destroy the JDBC connection.   * The driver remains.   * @param resource The Connection object   */   public void toErased(Object resource) {      Connection con = (Connection)resource;      try {         con.close();         log.info("JDBC connection closed for '" + this.dbUrl + "', '" + this.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      }   }   /**    * @see org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#update(String)    */   public int update(String command) throws Exception {      Connection  conn = null;      Statement   stmt = null;      try {         conn =  reserve();         conn.setAutoCommit(true);         stmt = conn.createStatement();         if (log.isLoggable(Level.FINE)) log.fine("Running update command '" + command + "'");         int rowsAffected = stmt.executeUpdate(command);         return rowsAffected;      }      catch (SQLException e) {         String str = "SQLException in query '" + command + "' : " + e;         log.warning(str + ": sqlSTATE=" + e.getSQLState() + " we destroy the connection in case it's stale");         //String sqlState = e.getSQLState(); // DatabaseMetaData method getSQLStateType can be used to discover whether the driver returns the XOPEN type or the SQL 99 type         // To be on the save side we always destroy the connection:         erase(conn);         conn = null;         throw e;      }      catch (Throwable e) {         e.printStackTrace();         String str = "Unexpected exception in query '" + command + "' : " + e;         log.severe(str + ": We destroy the connection in case it's stale");         erase(conn);         conn = null;         throw new Exception(e);      }      finally {         try {            if (stmt!=null) stmt.close();         }         catch (SQLException e) {            log.warning("Closing of stmt failed: " + e.toString());         }         if (conn!=null) release(conn);      }   }   /**    * @see org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#update(String)    */   public int update(Connection conn, String command) throws Exception {      if (conn == null)         return update(command);            Statement   stmt = null;      try {         stmt = conn.createStatement();         if (log.isLoggable(Level.FINE)) log.fine("Running update command '" + command + "'");         int rowsAffected = stmt.executeUpdate(command);         return rowsAffected;      }      catch (SQLException e) {         String str = "SQLException in query '" + command + "' : " + e;         log.warning(str + ": sqlSTATE=" + e.getSQLState());         throw e;      }      catch (Throwable e) {         e.printStackTrace();         String str = "Unexpected exception in query '" + command + "' : " + e;         log.severe(str);         throw new Exception(e);      }      finally {         try {            if (stmt!=null) stmt.close();         }         catch (SQLException e) {            log.warning("Closing of stmt failed: " + e.toString());         }      }   }   /**    * @see org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#select(String, I_ResultCb)    */   public void select(String command, I_ResultCb cb) throws Exception {      select(null, command, true, cb);   }   /**    * @see org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#select(java.sql.Connection, String, I_ResultCb)    */   public Connection select(Connection connection, String command, I_ResultCb cb) throws Exception {      return select(connection, command, false, cb);   }      /**    * @see org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#select(java.sql.Connection, String, I_ResultCb, boolean)    */   public Connection select(Connection connection, String command, boolean autoCommit, I_ResultCb cb) throws Exception {      Statement stmt = null;      ResultSet rs = null;      Connection conn = null;      try {         conn = (connection == null) ? reserve() : connection;         conn.setAutoCommit(autoCommit);         stmt = conn.createStatement();         if (log.isLoggable(Level.FINE)) log.fine("Running " + (autoCommit?"autoCommit":"in "+((connection==null)?"new ":"")+"open transaction") + " command '" + command + "'");         rs = stmt.executeQuery(command);         cb.result(conn, rs);      }      catch (SQLException e) {         if (e.getSQLState() != null && e.getSQLState().indexOf("42000") != -1             && e.toString().indexOf("ORA-00942") != -1) { // ORA-00942: table or view does not exist TODO: How to make this portable???            // sqlStateXOpen=1, sqlStateSQL99=2 (Oracle 10g returns 0)            //log.fine("SQLStateType=" + conn.getMetaData().getSQLStateType());            log.fine("No db change detected, the table does not exist: " + e.toString());            cb.result(conn, null);            return conn;         }         String str = "SQLException in query '" + command + "' : " + e;         log.warning(str + ": sqlSTATE=" + e.getSQLState() + " we destroy the connection in case it's stale");         // To be on the save side we always destroy the connection:         if (connection == null && conn != null && !conn.getAutoCommit()) conn.rollback();         erase(conn);         conn = null;         throw e;      }      catch (Throwable e) {         if (e instanceof NullPointerException) {            e.printStackTrace();         }         String str = "Unexpected exception in query '" + command + "' : " + e;         log.severe(str + ": We destroy the connection in case it's stale");         if (connection == null && conn != null && !conn.getAutoCommit()) conn.rollback();         erase(conn);         conn = null;         throw new Exception(e);      }      finally {         try {            if (stmt!=null) stmt.close();         }         catch (SQLException e) {            log.warning("Closing of stmt failed: " + e.toString());         }      }            if (autoCommit) {         if (conn!=null) release(conn);         conn = null;      }                  return conn;   }   /**    * @see org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#shutdown    */   public synchronized void shutdown() {      this.initCount--;      if (this.initCount > 0)         return;      destroy();   }   /**    * may be empty and just contains db.* properties    * @return    */   private I_Info getInfo() {      return this.info;   }      public static void main(String[] args) {      try {         I_DbPool pool = new DbPool();         I_Info info = new PropertiesInfo(System.getProperties());         String filename = info.get("file", null);         if (filename == null) {            System.out.println("usage: java -Dfile=someFile org.xmlBlaster.contrib.db.DbPool");            System.exit(-1);         }                     pool.init(info);                  BufferedReader br = new BufferedReader(new FileReader(filename));         String line = null;         StringBuffer buf = new StringBuffer();         while (  (line = br.readLine()) != null)            buf.append(line).append("\n");         br.close();         String cmd = buf.toString();         System.out.println(cmd);         pool.update(buf.toString());         pool.shutdown();      }      catch (Exception ex) {               }   }   }

⌨️ 快捷键说明

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