amberconnection.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,682 行 · 第 1/5 页
JAVA
2,682 行
return false; } return true; } /** * Callback when the user transaction begins */ public void begin(Transaction xa) { try { xa.registerSynchronization(this); _isInTransaction = true; _isXA = true; } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } } /** * Starts a transaction. */ public void beginTransaction() throws SQLException { _isInTransaction = true; if (_conn != null && _isAutoCommit) { _isAutoCommit = false; _conn.setAutoCommit(false); } // _xid = _factory.getXid(); } /** * Sets XA. */ public void setXA(boolean isXA) { _isXA = isXA; _isInTransaction = isXA; if (isXA && ! _isRegistered) register(); } /** * Commits a transaction. */ public void commit() throws SQLException { if (log.isLoggable(Level.FINER)) log.log(Level.FINER, "AmberConnection.commit"); try { flushInternal(); _xid = 0; if (_conn != null) { _conn.commit(); } } catch (RuntimeException e) { throw e; } catch (SQLException e) { throw new IllegalStateException(e); } catch (Exception e) { throw new EJBExceptionWrapper(e); } finally { if (! _isXA) _isInTransaction = false; for (int i = 0; i < _txEntitiesTop; i++) { Entity entity = _txEntities[i]; entity.__caucho_afterCommit(); } if (log.isLoggable(Level.FINER)) log.log(Level.FINER, "cleaning up txEntities"); _txEntitiesTop = 0; } } /** * Callback before a utrans commit. */ public void beforeCompletion() { if (log.isLoggable(Level.FINER)) log.log(Level.FINER, this + " beforeCompletion"); try { beforeCommit(); // XXX: need to figure out how to throw JPA exceptions at commit() time. // } catch (SQLException e) { // throw e; } catch (RuntimeException e) { // jpa/0ga5 throw e; } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } } /** * Callback after a utrans commit. */ public void afterCompletion(int status) { if (log.isLoggable(Level.FINER)) { if (status == Status.STATUS_COMMITTED) log.finer(this + " afterCompletion(commit)"); else log.finer(this + " afterCompletion(rollback)"); } afterCommit(status == Status.STATUS_COMMITTED); _isXA = false; _isInTransaction = false; _isRegistered = false; // ejb/0d19 } /** * Called before the commit phase */ public void beforeCommit() throws SQLException { if (log.isLoggable(Level.FINER)) log.finer(this + " beforeCommit"); try { flushInternal(); } catch (SQLException e) { throw e; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } /* // jpa/0gh0 for (int i = _txEntities.size() - 1; i >= 0; i--) { Entity entity = _txEntities.get(i); // jpa/1500 if (entity.__caucho_getEntityState() == EntityState.P_DELETED) { EntityType entityType = entity.__caucho_getEntityType(); Object key = entity.__caucho_getPrimaryKey(); EntityItem item = _persistenceUnit.getEntity(entityType, key); if (item == null) { // jpa/0ga8: entity has been removed and DELETE SQL was already flushed. continue; } } entity.__caucho_flush(); } */ } /** * Commits a transaction. */ public void afterCommit(boolean isCommit) { try { if (log.isLoggable(Level.FINER)) log.log(Level.FINER, "AmberConnection.afterCommit: " + isCommit); if (! _isXA) _isInTransaction = false; if (isCommit) { if (_completionList.size() > 0) { _persistenceUnit.complete(_completionList); } } // jpa/0k20: clears the completion list in the // finally block so callbacks do not add a completion // which has been just removed. // // _completionList.clear(); for (int i = 0; i < _txEntitiesTop; i++) { Entity entity = _txEntities[i]; try { if (isCommit) entity.__caucho_afterCommit(); else entity.__caucho_afterRollback(); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } } if (log.isLoggable(Level.FINER)) log.log(Level.FINER, "cleaning up txEntities"); _txEntitiesTop = 0; // jpa/0s2k Entity []entities = _entities; for (int i = _entitiesTop - 1; i >= 0; i--) { // XXX: needs to check EXTENDED type. // jpa/0h07: persistence context TRANSACTION type. entities[i].__caucho_detach(); } // jpa/0h60 _entitiesTop = 0; // if (! isCommit) { // jpa/0j5c /* XXX: jpa/0k11 - avoids double rollback() Rollback is done from com.caucho.transaction.TransactionImpl to the pool item com.caucho.jca.PoolItem try { if (_conn != null) _conn.rollback(); } catch (SQLException e) { throw new IllegalStateException(e); } */ // } } finally { _completionList.clear(); } } public PersistenceException rollback(Exception e) { try { rollback(); } catch (Exception e1) { log.log(Level.FINE, e1.toString(), e1); } return new PersistenceException(e); } /** * Rollbacks a transaction. */ public void rollback() throws SQLException { if (log.isLoggable(Level.FINER)) log.log(Level.FINER, "AmberConnection.rollback"); try { flushInternal(); _xid = 0; if (_conn != null) { _conn.rollback(); } } catch (RuntimeException e) { throw e; } catch (SQLException e) { throw new IllegalStateException(e); } catch (Exception e) { throw new EJBExceptionWrapper(e); } finally { if (! _isXA) _isInTransaction = false; _completionList.clear(); for (int i = 0; i < _txEntitiesTop; i++) { Entity entity = _txEntities[i]; entity.__caucho_afterRollback(); } _txEntitiesTop = 0; } } /** * Flushes managed entities. */ public void flush() { try { checkTransactionRequired("flush"); flushInternal(); } catch (RuntimeException e) { throw e; } catch (SQLException e) { throw new IllegalStateException(e); } catch (Exception e) { throw new EJBExceptionWrapper(e); } } /** * Flushes managed entities. */ public void flushNoChecks() { try { flushInternal(); } catch (RuntimeException e) { throw e; } catch (SQLException e) { throw new IllegalStateException(e); } catch (Exception e) { throw new EJBExceptionWrapper(e); } } /** * Expires the entities */ public void expire() throws SQLException { Entity []entities = _entities; for (int i = _entitiesTop - 1; i >= 0; i--) { Entity entity = entities[i]; // jpa/0j5e if (! entity.__caucho_getEntityState().isPersist()) entity.__caucho_expire(); } } /** * Returns the connection. */ public Connection getConnection() throws SQLException { DataSource readDataSource = _persistenceUnit.getReadDataSource(); if (! _isXA && ! _isInTransaction && readDataSource != null) { if (_readConn == null) { _readConn = readDataSource.getConnection(); } else if (_readConn.isClosed()) { closeConnectionImpl(); _readConn = _persistenceUnit.getDataSource().getConnection(); } return _readConn; } if (_conn == null) { _conn = _persistenceUnit.getDataSource().getConnection(); _isAutoCommit = true; } else if (_conn.isClosed()) { closeConnectionImpl(); _conn = _persistenceUnit.getDataSource().getConnection(); _isAutoCommit = true; } if (_isXA) { } else if (_isInTransaction && _isAutoCommit) { _isAutoCommit = false; _conn.setAutoCommit(false); } else if (! _isInTransaction && ! _isAutoCommit) { _isAutoCommit = true; _conn.setAutoCommit(true); } return _conn; } /** * Prepares a statement. */ public PreparedStatement prepareStatement(String sql) throws SQLException { try { PreparedStatement pstmt = _preparedStatementMap.get(sql); if (pstmt == null) { Connection conn = getConnection(); // XXX: avoids locking issues. if (_statements.size() > 0) { conn = _statements.get(0).getConnection(); } // XXX: avoids locking issues. // See com.caucho.sql.UserConnection pstmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); _statements.add(pstmt); _preparedStatementMap.put(sql, pstmt); } return pstmt; } catch (SQLException e) { closeConnectionImpl(); throw e; } } /** * Closes a statement. */ public void closeStatement(String sql) throws SQLException { PreparedStatement pstmt = _preparedStatementMap.remove(sql); if (pstmt != null) { _statements.remove(pstmt); pstmt.close(); } } public static void close(ResultSet rs) { try { if (rs != null) rs.close(); } catch (SQLException e) { throw new AmberRuntimeException(e); } } /** * Prepares an insert statement. */ public PreparedStatement prepareInsertStatement(String sql, boolean isGeneratedId) throws SQLException { PreparedStatement pstmt = null; try { pstmt = _preparedStatementMap.get(sql); if (pstmt != null) return pstmt; Connection conn = getConnection(); // XXX: avoids locking issues. if (_statements.size() > 0) { conn = _statements.get(0).getConnection(); } if (isGeneratedId && _persistenceUnit.hasReturnGeneratedKeys()) pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); else { // XXX: avoids locking issues. // See com.caucho.sql.UserConnection pstmt = conn.prepareStatement(sql); } _statements.add(pstmt); _preparedStatementMap.put(sql, pstmt); return pstmt; } catch (SQLException e) { closeStatement(sql); throw e; } } /** * Updates the database with the values in object. If the object does * not exist, throws an exception. * * @param obj the object to update */ public void update(Object obj) { } /** * Saves the object. * * @param obj the object to create */ public void create(Object obj) throws SQLException { // ejb/0g22 exception handling try { createInternal(obj); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new EJBExceptionWrapper(e); } } /** * Saves the object. * * @param obj the object to create */ public void create(String homeName, Object obj) throws SQLException { // ejb/0g22 exception handling try { createInternal(homeName, obj);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?