📄 rdbmentitylockstore.java
字号:
Timestamp ts = new Timestamp(lock.getExpirationTime().getTime()); String owner = lock.getLockOwner(); try { RDBMServices.PreparedStatement ps = new RDBMServices.PreparedStatement(conn, getAddSql()); try { ps.setInt(1, typeID.intValue()); // entity type ps.setString(2, key); // entity key ps.setInt(3, lockType); // lock type ps.setTimestamp(4, ts); // lock expiration ps.setString(5, owner); // lock owner log.debug( "RDBMEntityLockStore.primAdd(): " + ps); int rc = ps.executeUpdate(); if ( rc != 1 ) { String errString = "Problem adding " + lock; log.error( errString); throw new LockingException(errString); } } finally { if ( ps != null ) ps.close(); } } catch (java.sql.SQLException sqle) { log.error(sqle, sqle); throw sqle; }}/** * Delete the IEntityLock from the underlying store. * @param lock * @param conn the database connection */private void primDelete(IEntityLock lock, Connection conn) throws LockingException, SQLException{ Integer typeID = EntityTypes.getEntityTypeID(lock.getEntityType()); String key = lock.getEntityKey(); int lockType = lock.getLockType(); Timestamp ts = new Timestamp(lock.getExpirationTime().getTime()); String owner = lock.getLockOwner(); try { RDBMServices.PreparedStatement ps = new RDBMServices.PreparedStatement(conn, getDeleteLockSql()); try { ps.setInt(1, typeID.intValue()); // entity type ps.setString(2, key); // entity key ps.setTimestamp(3, ts); // lock expiration ps.setInt(4, lockType) ; // lock type ps.setString(5, owner); // lock owner log.debug( "RDBMEntityLockStore.primDelete(): " + ps); int rc = ps.executeUpdate(); log.debug("RDBMEntityLockStore.primDelete(): deleted " + rc + " lock(s)."); } finally { if ( ps != null ) ps.close(); } } catch (java.sql.SQLException sqle) { log.error(sqle, sqle); throw sqle; }}/** * Delete IEntityLocks from the underlying store that have expired as of * <code>expiration</code>. Params <code>entityType</code> and * <code>entityKey</code> are optional. * * @param expiration java.util.Date * @param entityType Class * @param entityKey String * @param conn Connection */private void primDeleteExpired(Date expiration, Class entityType, String entityKey, Connection conn)throws LockingException, SQLException{ Statement stmnt = null; ResultSet rs = null; Timestamp ts = new Timestamp(expiration.getTime()); StringBuffer buff = new StringBuffer(100); buff.append("DELETE FROM " + LOCK_TABLE + " WHERE " + EXPIRATION_TIME_COLUMN + LT); buff.append(printTimestamp(ts)); if ( entityType != null ) { Integer typeID = EntityTypes.getEntityTypeID(entityType); buff.append(" AND " + ENTITY_TYPE_COLUMN + EQ + typeID); } if ( entityKey != null ) { buff.append(" AND " + ENTITY_KEY_COLUMN + EQ + sqlQuote(entityKey)); } String sql = buff.toString(); log.debug("RDBMEntityLockStore.deleteExpired(): " + sql); try { stmnt = conn.createStatement(); int rc = stmnt.executeUpdate(sql); String msg = "Deleted " + rc + " expired locks."; log.debug("RDBMEntityLockStore.deleteExpired(): " + msg); } catch (SQLException sqle) { throw new LockingException("Problem deleting expired locks: " + sqle.getMessage()); } finally { if ( stmnt != null ) stmnt.close(); }} /** * Retrieve IEntityLocks from the underlying store. * @param sql String - the sql string used to select the entity lock rows. * @exception LockingException - wraps an Exception specific to the store. */private IEntityLock[] primSelect(String sql) throws LockingException{ Connection conn = null; Statement stmnt = null; ResultSet rs = null; List locks = new ArrayList(); log.debug("RDBMEntityLockStore.primSelect(): " + sql); try { conn = RDBMServices.getConnection(); stmnt = conn.createStatement(); try { rs = stmnt.executeQuery(sql); try { while ( rs.next() ) { locks.add(instanceFromResultSet(rs)); } } finally { rs.close(); } } finally { stmnt.close(); } } catch (SQLException sqle) { log.error(sqle, sqle); throw new LockingException("Problem retrieving EntityLocks " + sqle.getMessage()); } finally { RDBMServices.releaseConnection(conn); } return ((IEntityLock[])locks.toArray(new IEntityLock[locks.size()]));}/** * Updates the lock's <code>expiration</code> and <code>lockType</code> in the * underlying store. The SQL is over-qualified to make sure the row has not been * updated since the lock was last checked. * @param lock * @param newExpiration java.util.Date * @param newType Integer * @param conn Connection */private void primUpdate(IEntityLock lock, Date newExpiration, Integer newType, Connection conn)throws SQLException, LockingException{ Integer typeID = EntityTypes.getEntityTypeID(lock.getEntityType()); String key = lock.getEntityKey(); int oldLockType = lock.getLockType(); int newLockType = ( newType == null ) ? oldLockType : newType.intValue(); java.sql.Timestamp oldTs = new java.sql.Timestamp(lock.getExpirationTime().getTime()); java.sql.Timestamp newTs = new java.sql.Timestamp(newExpiration.getTime()); String owner = lock.getLockOwner(); try { RDBMServices.PreparedStatement ps = new RDBMServices.PreparedStatement(conn, getUpdateSql()); try { ps.setTimestamp(1, newTs); // new expiration ps.setInt(2, newLockType); // new lock type ps.setInt(3, typeID.intValue()); // entity type ps.setString(4, key); // entity key ps.setString(5, owner); // lock owner ps.setTimestamp(6, oldTs); // old expiration ps.setInt(7, oldLockType); // old lock type; log.debug( "RDBMEntityLockStore.primUpdate(): " + ps); int rc = ps.executeUpdate(); if ( rc != 1 ) { String errString = "Problem updating " + lock; log.error( errString); throw new LockingException(errString); } } finally { if ( ps != null ) ps.close(); } } catch (java.sql.SQLException sqle) { log.error(sqle, sqle); throw sqle; }} /** * Retrieve IEntityLocks from the underlying store. Any or all of the parameters * may be null. * @param entityType Class * @param entityKey String * @param lockType Integer - so we can accept a null value. * @param expiration Date * @param lockOwner String * @exception LockingException - wraps an Exception specific to the store. */private IEntityLock[] select (Class entityType, String entityKey, Integer lockType, Date expiration, String lockOwner )throws LockingException{ StringBuffer sqlQuery = new StringBuffer( getSelectSql() + " WHERE 1 = 1"); if ( entityType != null ) { Integer typeID = EntityTypes.getEntityTypeID(entityType); sqlQuery.append(" AND " + ENTITY_TYPE_COLUMN + EQ + typeID); } if ( entityKey != null ) { sqlQuery.append(" AND " + ENTITY_KEY_COLUMN + EQ + sqlQuote(entityKey)); } if ( lockType != null ) { sqlQuery.append(" AND " + LOCK_TYPE_COLUMN + EQ + lockType); } if ( expiration != null ) { Timestamp ts = new Timestamp(expiration.getTime()); sqlQuery.append(" AND " + EXPIRATION_TIME_COLUMN + EQ + printTimestamp(ts)); } if ( lockOwner != null ) { sqlQuery.append(" AND " + LOCK_OWNER_COLUMN + EQ + sqlQuote(lockOwner)); } return primSelect(sqlQuery.toString());} /** * Retrieve IEntityLocks from the underlying store. Expiration must not be null. * @param entityType Class * @param entityKey String * @param lockType Integer - so we can accept a null value. * @param lockOwner String * @exception LockingException - wraps an Exception specific to the store. */private IEntityLock[] selectUnexpired (Timestamp ts, Class entityType, String entityKey, Integer lockType, String lockOwner)throws LockingException{ StringBuffer sqlQuery = new StringBuffer(getSelectSql()); sqlQuery.append(" WHERE " + EXPIRATION_TIME_COLUMN + GT + printTimestamp(ts)); if ( entityType != null ) { Integer typeID = EntityTypes.getEntityTypeID(entityType); sqlQuery.append(" AND " + ENTITY_TYPE_COLUMN + EQ + typeID); } if ( entityKey != null ) { sqlQuery.append(" AND " + ENTITY_KEY_COLUMN + EQ + sqlQuote(entityKey)); } if ( lockType != null ) { sqlQuery.append(" AND " + LOCK_TYPE_COLUMN + EQ + lockType); } if ( lockOwner != null ) { sqlQuery.append(" AND " + LOCK_OWNER_COLUMN + EQ + sqlQuote(lockOwner)); } return primSelect(sqlQuery.toString());}/** * @return org.jasig.portal.concurrency.locking.RDBMEntityLockStore */public static synchronized IEntityLockStore singleton() throws LockingException{ if ( singleton == null ) { singleton = new RDBMEntityLockStore(); } return singleton;}/** * @return java.lang.String */private static java.lang.String sqlQuote(Object o){ return QUOTE + o + QUOTE;}/** * @param lock org.jasig.portal.groups.IEntityLock * @param newExpiration java.util.Date */public void update(IEntityLock lock, java.util.Date newExpiration)throws LockingException{ update(lock, newExpiration, null);}/** * Updates the lock's <code>expiration</code> and <code>lockType</code> in the * underlying store. Param <code>lockType</code> may be null. * @param lock * @param newExpiration java.util.Date * @param newLockType Integer */public void update(IEntityLock lock, Date newExpiration, Integer newLockType)throws LockingException{ Connection conn = null; try { conn = RDBMServices.getConnection(); if ( newLockType != null ) { primDeleteExpired(new Date(), lock.getEntityType(), lock.getEntityKey(), conn); } primUpdate(lock, newExpiration, newLockType, conn); } catch (SQLException sqle) { throw new LockingException("Problem updating " + lock + ": " + sqle.getMessage()); } finally { RDBMServices.releaseConnection(conn); }}/** * @return long */private static long getTimestampMillis(Timestamp ts){ if ( timestampHasMillis ) { return ts.getTime(); } else { return (ts.getTime() + ts.getNanos() / 1000000); }}/** * @return java.lang.String */private static java.lang.String printTimestamp(Timestamp ts){ return RDBMServices.sqlTimeStamp(getTimestampMillis(ts));}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -