📄 lockmanagerejb.java
字号:
/* * Copyright 2006-2007 Queplix Corp. * * Licensed under the Queplix Public License, Version 1.1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */package com.queplix.core.modules.eql.ejb;import com.queplix.core.error.GenericSystemException;import com.queplix.core.integrator.security.AccessRightsManager;import com.queplix.core.integrator.security.LogonSession;import com.queplix.core.integrator.security.User;import com.queplix.core.jxb.entity.Efield;import com.queplix.core.jxb.entity.Entity;import com.queplix.core.modules.config.ejb.CaptionManagerLocal;import com.queplix.core.modules.config.utils.SysPropertyManager;import com.queplix.core.modules.eql.EQLDRes;import com.queplix.core.modules.eql.EQLERes;import com.queplix.core.modules.eql.EQLResCell;import com.queplix.core.modules.eql.EQLResRecord;import com.queplix.core.modules.eql.error.EQLException;import com.queplix.core.modules.eql.error.EQLLockException;import com.queplix.core.modules.eql.error.EQLLockExpiredException;import com.queplix.core.integrator.security.NoSuchUserException;import com.queplix.core.utils.DateHelper;import com.queplix.core.utils.StringHelper;import com.queplix.core.utils.sql.SqlWrapper;import com.queplix.core.utils.sql.error.SQLDuplicateKeyException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Date;import java.util.List;/** * Locking operations manager. * @author [SVM] Maxim Suponya * @author [ALB] Baranov Andrey * @version $Revision: 1.1.1.1 $ $Date: 2005/09/12 15:30:25 $ */public class LockManagerEJB extends AbstractEQLSupportedEJB { // --------------------------------------------------------------- constants // Null key value protected static final String NULL_VALUE = StringHelper.NULL_VALUE; // --------------------------------------------------------------- public methods public void ejbCreate() {} /** * Checks lock record * @param eqlRes EQLERes response * @param ls logon session * @throws EQLException */ public void check( EQLERes eqlRes, LogonSession ls ) throws EQLException { // Initialization SqlWrapper sqlW = getSqlWrapper(); List<LockStructure> lockStructList = buildLockStructures( ls, eqlRes ); if( lockStructList.isEmpty() ) { return; } // Build SQL to check lock. StringBuffer sql = new StringBuffer( "SELECT * FROM QX_LOCK WHERE " ); sql.append( getSelectCondition( lockStructList ) ); if( getLogger().isDebugEnabled() ) { DEBUG( "Try to check records:" ); DEBUG( " list: " + lockStructList ); DEBUG( " sql: " + sql ); } // Execute SQL. Connection con = null; PreparedStatement ps = null; try { con = sqlW.doConnection(); ps = sqlW.doPreparedStatement( con, sql.toString() ); ResultSet rs = sqlW.executeQuery( ps ); boolean found = false; while( rs.next() ) { found = true; // retrive data LockStructure lockStruct = LockStructure.build( rs ); // check boolean isOwnLock = lockStruct.isOwn( ls, null, null ); if( !isOwnLock ) { // not own lock - throw exception throw new EQLLockExpiredException(); } } if( !found ) { // no lock(s) found - throw exception throw new EQLLockExpiredException(); } } catch( SQLException ex ) { ERROR( ex ); throw new GenericSystemException( ex ); } finally { sqlW.closeConnection( con, ps ); } } private void removeExpiredLocks() throws EQLException{ SqlWrapper sqlW = getSqlWrapper(); long lockTimeout = Long.parseLong( SysPropertyManager.getProperty( "LockTimeout" ) ); Connection con = null; PreparedStatement ps = null; try { con = sqlW.doConnection(); ps = sqlW.doPreparedStatement(con, "delete from qx_lock where created < ?"); Date delta = new Date(); delta.setTime(DateHelper.currentTimeMillis() - lockTimeout*1000); sqlW.getTimestampParser().setValue(ps, 1, delta); sqlW.executeUpdate(ps); } catch (SQLException e) { ERROR(e); setRollbackOnly(); throw new EQLException( "Cannot unlock expired record(s). Status code: " + e.getErrorCode()); } finally { sqlW.closeConnection( con, ps ); } } /** * Try to lock records with dataset records * @param eqlRes EQLERes response * @param focus web focus id * @param focusInstance web focus instance number * @param ls logon session * @throws EQLException */ public void lock( EQLERes eqlRes, String focus, Long focusInstance, LogonSession ls ) throws EQLException { removeExpiredLocks(); // Initialization SqlWrapper sqlW = getSqlWrapper(); List<LockStructure> lockStructList = buildLockStructures( ls, eqlRes, null, focus, focusInstance ); if( lockStructList.isEmpty() ) { return; } List<LockStructure> recordsForUpdate = new ArrayList<LockStructure>(); // Build SQL to check lock. StringBuffer sql = new StringBuffer( "SELECT * FROM QX_LOCK WHERE " ); sql.append( getSelectCondition( lockStructList ) ); if( getLogger().isDebugEnabled() ) { DEBUG( "Try to update records:" ); DEBUG( " list: " + lockStructList ); DEBUG( " sql: " + sql ); } // Execute SQL. Connection con = null; PreparedStatement ps = null; try { con = sqlW.doConnection(); ps = sqlW.doPreparedStatement( con, sql.toString() ); ResultSet rs = sqlW.executeQuery( ps ); while( rs.next() ) { // retrive data LockStructure lockStruct = LockStructure.build( rs ); // check boolean isOwnLock = lockStruct.isOwn( ls, focus, focusInstance ); if( getLogger().isDebugEnabled() ) { DEBUG( "Check lock:" ); DEBUG( " struct: " + lockStruct ); DEBUG( " own lock?: " + isOwnLock ); } if( isOwnLock) { // remember LockStructure for future update recordsForUpdate.add( lockStruct ); } else { // throws exception - record locked throw buildLockException( ls, lockStruct ); } } rs.close(); ps.close(); // Insert new records. if( !lockStructList.isEmpty() ) { sql = new StringBuffer( "INSERT INTO QX_LOCK (" ); sql.append( "PKEY," ); sql.append( "TABLE_NAME," ); sql.append( "RECORD_ID," ); sql.append( "RECORD_ID2," ); sql.append( "RECORD_ID3," ); sql.append( "RECORD_ID4," ); sql.append( "SESSION_ID," ); sql.append( "FOCUS_ID," ); sql.append( "FOCUS_INSTANCE," ); sql.append( "USER_ID," ); sql.append( "USER_TYPE_ID," ); sql.append( "CREATED" ); sql.append( ") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" ); ps = sqlW.doPreparedStatement( con, sql.toString() ); for( int i = 0; i < lockStructList.size(); i++ ) { LockStructure lockStruct = lockStructList.get( i ); if( recordsForUpdate.contains( lockStruct ) ) { // will be updated later continue; } if( getLogger().isDebugEnabled() ) { DEBUG( "Insert record:" ); DEBUG( " struct: " + lockStruct ); } ps.setLong( 1, getNextKeyLocal().next( "QX_LOCK" ) ); sqlW.getStringParser().setValue( ps, 2, lockStruct.tableName ); sqlW.getStringParser().setValue( ps, 3, lockStruct.recordIds.get( 0 )); sqlW.getStringParser().setValue( ps, 4, lockStruct.recordIds.get( 1 )); sqlW.getStringParser().setValue( ps, 5, lockStruct.recordIds.get( 2 )); sqlW.getStringParser().setValue( ps, 6, lockStruct.recordIds.get( 3 )); sqlW.getStringParser().setValue( ps, 7, lockStruct.session ); sqlW.getStringParser().setValue( ps, 8, lockStruct.focus ); sqlW.getLongParser().setValue( ps, 9, lockStruct.focusInstance ); sqlW.getLongParser().setValue( ps, 10, lockStruct.user ); sqlW.getIntParser().setValue( ps, 11, lockStruct.userType ); sqlW.getTimestampParser().setValue( ps, 12, DateHelper.getNowDate() ); try { sqlW.executeUpdate( ps ); } catch( SQLDuplicateKeyException ex ) { // Ops. somebody already locked this record. throw buildLockException( ls, lockStruct ); } } ps.close(); } // Update old records. if( !recordsForUpdate.isEmpty() ) { sql = new StringBuffer( "UPDATE QX_LOCK SET " ); sql.append( "SESSION_ID = ?," ); sql.append( "FOCUS_ID = ?," ); sql.append( "FOCUS_INSTANCE = ?, " ); sql.append( "CREATED = ? " ); sql.append( "WHERE PKEY IN (" ); for( int i = 0; i < recordsForUpdate.size(); i++ ) { if( i > 0 ) { sql.append( "," ); } LockStructure lockStruct = recordsForUpdate.get( i ); sql.append( lockStruct.pkey ); } sql.append( ")" ); if( getLogger().isDebugEnabled() ) { DEBUG( "Update records:" ); DEBUG( " list: " + recordsForUpdate ); DEBUG( " sql: " + sql ); } ps = sqlW.doPreparedStatement( con, sql.toString() ); sqlW.getStringParser().setValue( ps, 1, ls.getSessionID() ); sqlW.getStringParser().setValue( ps, 2, focus ); sqlW.getLongParser().setValue( ps, 3, focusInstance ); sqlW.getTimestampParser().setValue( ps, 4, DateHelper.getNowDate() ); sqlW.executeUpdate( ps ); ps.close(); } } catch( EQLLockException ex ) { // rollback entire transaction setRollbackOnly(); throw ex; } catch( SQLException ex ) { // rollback entire transaction /** @todo fix it in future release */ ERROR( ex ); setRollbackOnly(); throw new EQLException( "Cannot lock record(s). Status code: " + ex.getErrorCode() + ". Please, try again." ); } finally { sqlW.closeConnection( con, ps ); } } /** * Unlock record using session id from logon session * @param eqlRes EQLERes response * @param ls logon session * @throws EQLException */ public void unlock( EQLERes eqlRes, LogonSession ls ) throws EQLException { unlock( eqlRes, ls, null ); } /** * Unlocks record using session id from logon session * @param eqlRes EQLERes response * @param ls logon session * @param record EQLResRecord record * @throws EQLException */ public void unlock( EQLERes eqlRes, LogonSession ls, EQLResRecord record ) throws EQLException { // Initialization SqlWrapper sqlW = getSqlWrapper(); List<LockStructure> lockStructList = buildLockStructures( ls, eqlRes, record ); if( lockStructList.isEmpty() ) { return; } // Build SQL to delete lock. StringBuffer sql = new StringBuffer( "DELETE FROM QX_LOCK WHERE SESSION_ID = ?" ); String cond = getSelectCondition( lockStructList ); if( cond != null ) { sql.append( " AND " ).append( cond ); } // Execute SQL. Connection con = null; PreparedStatement ps = null; try { con = sqlW.doConnection(); ps = sqlW.doPreparedStatement( con, sql.toString() ); ps.setString( 1, ls.getSessionID() ); int records = sqlW.executeUpdate( ps ); if( getLogger().isDebugEnabled() ) { DEBUG( "Remove " + records + " records." ); } } catch( SQLException ex ) { throwException( "Cannot unlock record(s). Status code: " + ex.getErrorCode() + ".", ex ); } finally { sqlW.closeConnection( con, ps ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -