use of java.sql.Savepoint in project CloudStack-archive by CloudStack-extras.
the class Merovingian method release.
public boolean release(String key) {
boolean validLock = false;
try {
assert _locks.size() > 0 : "There are no locks here. Why are you trying to release " + key;
Ternary<Savepoint, Integer, Long> lock = _locks.get(key);
if (lock != null) {
validLock = true;
if (lock.second() > 1) {
lock.second(lock.second() - 1);
if (s_logger.isTraceEnabled()) {
s_logger.trace("Lock: Releasing " + key + " but not in DB " + lock.second());
}
return false;
}
if (s_logger.isDebugEnabled() && !_locks.keySet().iterator().next().equals(key)) {
s_logger.trace("Lock: Releasing out of order for " + key);
}
_locks.remove(key);
if (s_logger.isTraceEnabled()) {
s_logger.trace("Lock: Releasing " + key + " after " + (InaccurateClock.getTime() - lock.third()));
}
Connection conn = getConnection(key, true);
conn.rollback(lock.first());
} else {
s_logger.warn("Merovingian.release() is called against key " + key + " but the lock of this key does not exist!");
}
if (_locks.size() == 0) {
closeConnection();
}
} catch (SQLException e) {
s_logger.warn("unable to rollback for " + key);
} finally {
synchronized (s_memLocks) {
Pair<Lock, Integer> memLock = s_memLocks.get(key);
if (memLock != null) {
memLock.second(memLock.second() - 1);
if (memLock.second() <= 0) {
s_memLocks.remove(key);
}
if (validLock)
memLock.first().unlock();
} else {
throw new CloudRuntimeException("Merovingian.release() is called for key " + key + ", but its memory lock no longer exist! This is not good, guys");
}
}
}
return true;
}
use of java.sql.Savepoint in project CloudStack-archive by CloudStack-extras.
the class Transaction method setSavepoint.
public Savepoint setSavepoint() throws SQLException {
_txn = true;
StackElement st = new StackElement(START_TXN, null);
_stack.push(st);
final Connection conn = getConnection();
final Savepoint sp = conn.setSavepoint();
st.ref = sp;
return sp;
}
use of java.sql.Savepoint in project aries by apache.
the class ScopedConnectionWrapperTest method testRollbackSavepoint.
@Test
public void testRollbackSavepoint() throws SQLException {
Savepoint s = Mockito.mock(Savepoint.class);
Connection wrapped = new ScopedConnectionWrapper(conn);
wrapped.rollback(s);
Mockito.verify(conn).rollback(s);
}
use of java.sql.Savepoint in project aries by apache.
the class ScopedConnectionWrapperTest method testReleaseSavepoint.
@Test
public void testReleaseSavepoint() throws SQLException {
Savepoint s = Mockito.mock(Savepoint.class);
Connection wrapped = new ScopedConnectionWrapper(conn);
wrapped.releaseSavepoint(s);
Mockito.verify(conn).releaseSavepoint(s);
}
use of java.sql.Savepoint in project adempiere by adempiere.
the class Migrate method cleanupADSequences.
/**
* correct counters in AD_Sequence
*/
private void cleanupADSequences() {
// only continue if we have required tables
if (!m_source.isObjectExists("AD_Sequence", m_source.getTables()))
return;
if (!m_source.isObjectExists("AD_Table", m_source.getTables()))
return;
if (!m_source.isObjectExists("AD_Column", m_source.getTables()))
return;
// In Adempiere, sequences can be held in native DB sequence objects,
// not just in the AD_Sequence table, so we also have to check those.
// Since sequences may have been incremented during data
// synchronization,
// we can not rely on the metadata we have loaded in the source and
// target
// connections.
// The easiest solution is probably to just reload the sequence metadata
// from
// the migrated target, and since sequences are not too complex,
// it should not result in a big performance loss.
HashMap<String, DBObject> currentSequences = new HashMap<String, DBObject>(m_target.reloadSequences());
// reset DB objects
resetDBObjects(null);
s_logger.log(Level.CONFIG, "");
s_logger.log(Level.CONFIG, "cleanupADSequences", m_direction);
String vendor = m_target.getVendor();
String catalog = m_target.getCatalog();
String schema = m_target.getSchema();
// remember savepoint for rollback
Savepoint sp = m_target.setSavepoint("synchronize sequences");
// prepare statements for sequence manipulation
ArrayList<String> insertColumnNames = new ArrayList<String>();
ArrayList<String> updateColumnNames = new ArrayList<String>();
ArrayList<String> updateWhereColumnNames = new ArrayList<String>();
// we are only interested in the structure of sequences as defined in
// the data dictionary
// and can ignore customizations, so it is sufficient to only look at
// the structure
// of source objects
DBObject sequenceTable = m_source.getObjectByName("AD_Sequence", m_source.getTables());
Vector<Integer> v = new Vector<Integer>(sequenceTable.getContents().keySet());
java.util.Collections.sort(v);
for (Iterator<Integer> columnIterator = v.iterator(); columnIterator.hasNext(); ) {
Integer key = columnIterator.next();
DBObject_Table_Column sequenceColumn = (DBObject_Table_Column) sequenceTable.getContents().get(key);
String columnName = sequenceColumn.getName();
// columns required for inserting new sequence counters
if (columnName.equalsIgnoreCase("AD_Sequence_ID") || columnName.equalsIgnoreCase("AD_Client_ID") || columnName.equalsIgnoreCase("AD_Org_ID") || columnName.equalsIgnoreCase("CreatedBy") || columnName.equalsIgnoreCase("UpdatedBy") || columnName.equalsIgnoreCase("Name") || columnName.equalsIgnoreCase("Description") || columnName.equalsIgnoreCase("IncrementNo") || columnName.equalsIgnoreCase("StartNo") || columnName.equalsIgnoreCase("CurrentNext") || columnName.equalsIgnoreCase("CurrentNextSys") || columnName.equalsIgnoreCase("isTableId")) {
insertColumnNames.add(columnName);
}
// columns required for updating user sequence counters
if (columnName.equalsIgnoreCase("Updated") || columnName.equalsIgnoreCase("CurrentNext") || columnName.equalsIgnoreCase("CurrentNextSys")) {
updateColumnNames.add(columnName);
}
// columns to use in WHERE clause for updating sequence counters
if (columnName.equalsIgnoreCase("AD_Sequence_ID")) {
updateWhereColumnNames.add(columnName);
}
}
PreparedStatementWrapper stmtInsertSequence = m_target.setPreparedStatement(s_dbEngine.sql_insertPreparedStatement(vendor, catalog, schema, "AD_Sequence", insertColumnNames));
PreparedStatementWrapper stmtUpdateSequence = m_target.setPreparedStatement(s_dbEngine.sql_updatePreparedStatement(vendor, catalog, schema, "AD_Sequence", updateColumnNames, updateWhereColumnNames));
PreparedStatementWrapper stmtLoadUnsequencedDocuments = m_target.setPreparedStatement(s_dbEngine.sqlAD_getUnsequencedDocuments(vendor, catalog, schema));
// get counters for AD_Sequence itself
int adSequenceNextSys = 0;
int adSequenceNextUser = 0;
int adSequenceIncrement = 0;
ArrayList<String> whereColumnNames = new ArrayList<String>();
whereColumnNames.add("name");
String sqlCommand = s_dbEngine.sql_selectPreparedStatement(vendor, catalog, schema, "AD_Sequence", whereColumnNames);
PreparedStatementWrapper pstmt = m_target.setPreparedStatement(sqlCommand);
m_target.setPreparedStatementString(pstmt, 1, "AD_Sequence");
ResultSet rs = m_target.executeQuery(pstmt);
if (m_target.getResultSetNext(rs)) {
adSequenceIncrement = m_target.getResultSetInt(rs, "IncrementNo");
}
m_target.releaseResultSet(rs);
m_target.releasePreparedStatement(pstmt);
sqlCommand = s_dbEngine.sqlAD_getSequenceMaxUser(vendor, catalog, schema, "AD_Sequence", "AD_Sequence_ID");
Statement stmt = m_target.setStatement();
rs = m_target.executeQuery(stmt, sqlCommand);
if (m_target.getResultSetNext(rs)) {
adSequenceNextUser = m_target.getResultSetInt(rs, "MAX_SEQ") + adSequenceIncrement;
}
m_target.releaseResultSet(rs);
m_target.releaseStatement(stmt);
sqlCommand = s_dbEngine.sqlAD_getSequenceMaxSystem(vendor, catalog, schema, "AD_Sequence", "AD_Sequence_ID");
stmt = m_target.setStatement();
rs = m_target.executeQuery(stmt, sqlCommand);
if (m_target.getResultSetNext(rs)) {
adSequenceNextSys = m_target.getResultSetInt(rs, "MAX_SEQ") + adSequenceIncrement;
}
m_target.releaseResultSet(rs);
m_target.releaseStatement(stmt);
// add missing AD_Sequence records for tables
int ad_client_id = 0;
String tableName = "";
sqlCommand = s_dbEngine.sqlAD_getUnsequencedTables(vendor, catalog, schema);
stmt = m_target.setStatement();
rs = m_target.executeQuery(stmt, sqlCommand);
while (m_target.getResultSetNext(rs)) {
ad_client_id = m_target.getResultSetInt(rs, "AD_Client_ID");
tableName = m_target.getResultSetString(rs, "TableName");
int nextSeq = 0;
if (ad_client_id < s_parameters.MINUSERLEVELID) {
nextSeq = adSequenceNextSys;
adSequenceNextSys += adSequenceIncrement;
} else {
nextSeq = adSequenceNextUser;
adSequenceNextUser += adSequenceIncrement;
}
for (int i = 0; i < insertColumnNames.size(); i++) {
String columnName = insertColumnNames.get(i);
int parameterIndex = i + 1;
if (columnName.equalsIgnoreCase("AD_Sequence_ID")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, nextSeq);
} else if (columnName.equalsIgnoreCase("AD_Client_ID")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, ad_client_id);
} else if (columnName.equalsIgnoreCase("AD_Org_ID")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 0);
} else if (columnName.equalsIgnoreCase("CreatedBy")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 0);
} else if (columnName.equalsIgnoreCase("UpdatedBy")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 0);
} else if (columnName.equalsIgnoreCase("Name")) {
m_target.setPreparedStatementString(stmtInsertSequence, parameterIndex, tableName);
} else if (columnName.equalsIgnoreCase("Description")) {
m_target.setPreparedStatementString(stmtInsertSequence, parameterIndex, tableName);
} else if (columnName.equalsIgnoreCase("IncrementNo")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 1);
} else if (columnName.equalsIgnoreCase("StartNo")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 1000000);
} else if (columnName.equalsIgnoreCase("CurrentNext")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 1000000);
} else if (columnName.equalsIgnoreCase("CurrentNextSys")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 100);
} else if (columnName.equalsIgnoreCase("isTableId")) {
m_target.setPreparedStatementString(stmtInsertSequence, parameterIndex, "Y");
}
}
Integer sqlResult = m_target.executeUpdate(stmtInsertSequence, false);
if (sqlResult != null) {
logAddDetail(sqlResult, null);
}
}
m_target.releaseResultSet(rs);
m_target.releaseStatement(stmt);
// add missing AD_Sequence records for document numbers
// load clients
String sqlLoadClients = s_dbEngine.sql_select(vendor, catalog, schema, "AD_Client");
stmt = m_target.setStatement();
ResultSet rsLoadClients = m_target.executeQuery(stmt, sqlLoadClients);
while (m_target.getResultSetNext(rsLoadClients)) {
ad_client_id = m_target.getResultSetInt(rsLoadClients, "AD_Client_ID");
// load unsequenced documents
m_target.setPreparedStatementInt(stmtLoadUnsequencedDocuments, 1, ad_client_id);
ResultSet rsLoadDocuments = m_target.executeQuery(stmtLoadUnsequencedDocuments);
while (m_target.getResultSetNext(rsLoadDocuments)) {
tableName = m_target.getResultSetString(rsLoadDocuments, "TableName");
String documentName = new StringBuffer("DocumentNo_").append(tableName).toString();
String documentDescription = new StringBuffer("DocumentNo/Value for table ").append(tableName).toString();
int nextSeq = 0;
if (ad_client_id < s_parameters.MINUSERLEVELID) {
nextSeq = adSequenceNextSys;
adSequenceNextSys += adSequenceIncrement;
} else {
nextSeq = adSequenceNextUser;
adSequenceNextUser += adSequenceIncrement;
}
for (int i = 0; i < insertColumnNames.size(); i++) {
String columnName = insertColumnNames.get(i);
int parameterIndex = i + 1;
if (columnName.equalsIgnoreCase("AD_Sequence_ID")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, nextSeq);
} else if (columnName.equalsIgnoreCase("AD_Client_ID")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, ad_client_id);
} else if (columnName.equalsIgnoreCase("AD_Org_ID")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 0);
} else if (columnName.equalsIgnoreCase("CreatedBy")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 0);
} else if (columnName.equalsIgnoreCase("UpdatedBy")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 0);
} else if (columnName.equalsIgnoreCase("Name")) {
m_target.setPreparedStatementString(stmtInsertSequence, parameterIndex, documentName);
} else if (columnName.equalsIgnoreCase("Description")) {
m_target.setPreparedStatementString(stmtInsertSequence, parameterIndex, documentDescription);
} else if (columnName.equalsIgnoreCase("IncrementNo")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 1);
} else if (columnName.equalsIgnoreCase("StartNo")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 1000000);
} else if (columnName.equalsIgnoreCase("CurrentNext")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 1000000);
} else if (columnName.equalsIgnoreCase("CurrentNextSys")) {
m_target.setPreparedStatementInt(stmtInsertSequence, parameterIndex, 1000000);
} else if (columnName.equalsIgnoreCase("isTableId")) {
m_target.setPreparedStatementString(stmtInsertSequence, parameterIndex, "N");
}
}
Integer sqlResult = m_target.executeUpdate(stmtInsertSequence, false);
if (sqlResult != null) {
logAddDetail(sqlResult, null);
}
}
m_target.releaseResultSet(rsLoadDocuments);
}
m_target.releaseResultSet(rsLoadClients);
m_target.releaseStatement(stmt);
// check and correct sequence numbers of tables
// (this also automatically corrects AD_Sequence itself after above
// changes)
whereColumnNames = new ArrayList<String>();
whereColumnNames.add("IsTableID");
sqlCommand = s_dbEngine.sql_selectPreparedStatement(vendor, catalog, schema, "AD_Sequence", whereColumnNames);
PreparedStatementWrapper stmtLoadSequences = m_target.setPreparedStatement(sqlCommand);
m_target.setPreparedStatementString(stmtLoadSequences, 1, "Y");
ResultSet rsLoadSequences = m_target.executeQuery(stmtLoadSequences);
while (m_target.getResultSetNext(rsLoadSequences)) {
int ad_sequence_id = m_target.getResultSetInt(rsLoadSequences, "AD_Sequence_ID");
tableName = m_target.getResultSetString(rsLoadSequences, "Name");
String columnName = new StringBuffer(tableName).append("_ID").toString();
int incrementNo = m_target.getResultSetInt(rsLoadSequences, "IncrementNo");
long currentNext = m_target.getResultSetLong(rsLoadSequences, "CurrentNext");
long currentNextSys = m_target.getResultSetLong(rsLoadSequences, "CurrentNextSys");
ad_client_id = m_target.getResultSetInt(rsLoadSequences, "AD_Client_ID");
// check whether table has a column called tablename_id which
// contains a numeric value
DBObject table = m_source.getObjectByName(tableName, m_source.getTables());
String checkVendor = m_source.getVendor();
// target
if (table == null) {
table = m_target.getObjectByName(tableName, m_target.getTables());
checkVendor = m_target.getVendor();
// ignore the target table if it is not customized
if (table != null && table.getCustomizationLevel() == s_parameters.CUSTOMNONE)
table = null;
}
// ignore it
if (table != null) {
// to hold the sequence number
if (!hasTableColumn(table, columnName))
columnName = "seqno";
// and check the sequence
if (hasTableColumn(table, columnName)) {
// does tablename_ID contain a numeric value?
boolean isIDNumeric = false;
for (Iterator<Integer> colIterator = table.getContents().keySet().iterator(); colIterator.hasNext(); ) {
Integer key = colIterator.next();
DBObject_Table_Column col = (DBObject_Table_Column) table.getContents().get(key);
if (columnName.equalsIgnoreCase(col.getName())) {
int columnType = s_dbEngine.getDataTypeID(checkVendor, col.getType());
if (columnType < s_dbEngine.CHARTYPE_START)
isIDNumeric = true;
}
}
if (isIDNumeric) {
// get highest existing user sequence
long maxUserSequence = 0;
sqlCommand = s_dbEngine.sqlAD_getSequenceMaxUser(vendor, catalog, schema, tableName, columnName);
stmt = m_target.setStatement();
rs = m_target.executeQuery(stmt, sqlCommand);
if (m_target.getResultSetNext(rs)) {
maxUserSequence = m_target.getResultSetLong(rs, "MAX_SEQ");
}
m_target.releaseResultSet(rs);
m_target.releaseStatement(stmt);
if (maxUserSequence == 0)
maxUserSequence = 1000000;
else
maxUserSequence += incrementNo;
// get highest existing system sequence
long maxSysSequence = 0;
sqlCommand = s_dbEngine.sqlAD_getSequenceMaxSystem(vendor, catalog, schema, tableName, columnName);
stmt = m_target.setStatement();
rs = m_target.executeQuery(stmt, sqlCommand);
if (m_target.getResultSetNext(rs)) {
maxSysSequence = m_target.getResultSetLong(rs, "MAX_SEQ");
}
m_target.releaseResultSet(rs);
m_target.releaseStatement(stmt);
if (maxSysSequence == 0)
maxSysSequence = 100;
else
maxSysSequence += incrementNo;
// check if any native database sequences are defined
// for this table
String nativeSequenceName = new StringBuffer(tableName).append("_SEQ").toString().toUpperCase();
DBObject nativeSequenceObject = null;
DBObject_Sequence_Counter nativeSequenceCounter = null;
long nativeSequenceValue = 0;
long checkSeq = 0;
if (m_target.isObjectExists(nativeSequenceName, currentSequences)) {
nativeSequenceObject = m_target.getObjectByName(nativeSequenceName, currentSequences);
nativeSequenceCounter = (DBObject_Sequence_Counter) nativeSequenceObject.getContents().get(0);
nativeSequenceValue = nativeSequenceCounter.getCurrent();
// next value as same
if (ad_client_id < s_parameters.MINUSERLEVELID) {
checkSeq = currentNextSys;
if (maxSysSequence < nativeSequenceValue)
maxSysSequence = nativeSequenceValue;
} else {
checkSeq = currentNext;
if (maxUserSequence < nativeSequenceValue)
maxUserSequence = nativeSequenceValue;
}
}
// migration
if (isPreserveTableIDs()) {
long previousSystemSequence = 0;
long previousUserSequence = 0;
HashMap<String, Long> previousSequences = m_target.getSequenceSystem();
if (previousSequences.containsKey(tableName.toUpperCase()))
previousSystemSequence = previousSequences.get(tableName.toUpperCase());
if (maxSysSequence < previousSystemSequence)
maxSysSequence = previousSystemSequence;
previousSequences = m_target.getSequenceUser();
if (previousSequences.containsKey(tableName.toUpperCase()))
previousUserSequence = previousSequences.get(tableName.toUpperCase());
if (maxUserSequence < previousUserSequence)
maxUserSequence = previousUserSequence;
}
// in native database sequence
if (currentNext < maxUserSequence || currentNextSys < maxSysSequence || nativeSequenceValue != checkSeq) {
if (currentNext < maxUserSequence)
currentNext = maxUserSequence;
if (currentNextSys < maxSysSequence)
currentNextSys = maxSysSequence;
// the correct sequence values
for (int i = 0; i < updateColumnNames.size(); i++) {
String updateColumnName = updateColumnNames.get(i);
int parameterIndex = i + 1;
if (updateColumnName.equalsIgnoreCase("Updated")) {
m_target.setPreparedStatementTimestamp(stmtUpdateSequence, parameterIndex, new java.sql.Timestamp(System.currentTimeMillis()));
} else if (updateColumnName.equalsIgnoreCase("CurrentNext")) {
m_target.setPreparedStatementLong(stmtUpdateSequence, parameterIndex, currentNext);
} else if (updateColumnName.equalsIgnoreCase("CurrentNextSys")) {
m_target.setPreparedStatementLong(stmtUpdateSequence, parameterIndex, currentNextSys);
}
}
// the ad_sequence record to update
m_target.setPreparedStatementInt(stmtUpdateSequence, updateColumnNames.size() + 1, ad_sequence_id);
Integer sqlResult = m_target.executeUpdate(stmtUpdateSequence, false);
if (sqlResult != null) {
logUpdateDetail(sqlResult, null);
}
// update native database sequence if necessary
if (nativeSequenceObject != null) {
long nextSeq = currentNextSys;
if (ad_client_id >= s_parameters.MINUSERLEVELID)
nextSeq = currentNext;
if (nextSeq != nativeSequenceValue) {
// set the new counter value
nativeSequenceCounter.setCurrent(nextSeq);
// (containing the new value)
if (nativeSequenceObject.update(nativeSequenceObject))
logUpdateDetail(1, null);
}
}
}
}
}
}
}
m_target.releaseResultSet(rsLoadSequences);
m_target.releasePreparedStatement(stmtLoadSequences);
// no way to check sequence numbers of documents.
// some docs use BP's numbers which would mess up ad_sequence.
// but anyway, document numbers are not used by system clients, so they
// were not
// overwritten during migration and we can just keep what we have.
// close prepared statements
m_target.releasePreparedStatement(stmtInsertSequence);
m_target.releasePreparedStatement(stmtUpdateSequence);
m_target.releasePreparedStatement(stmtLoadUnsequencedDocuments);
// release savepoint
m_target.releaseSavepoint(sp);
logResults();
}
Aggregations