Purge persistence data at once instead of separate queries per instance

master
Vitaliy Filippov 2015-12-22 14:12:21 +03:00
parent 647dbc8522
commit b842af8296
3 changed files with 25 additions and 95 deletions

View File

@ -79,35 +79,32 @@ public class PurgeData {
AbstractDBConnection dbConn = null; AbstractDBConnection dbConn = null;
Statement stmt = null; Statement stmt = null;
try { try {
LOGGER.log(Level.INFO, I18n.loc("BPCOR-6152: Purging persistence data"));
dbConn = connFac.createNonXAConnection(); dbConn = connFac.createNonXAConnection();
Connection conn = dbConn.getUnderlyingConnection(); Connection conn = dbConn.getUnderlyingConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement(); stmt = conn.createStatement();
String status = StateDBO.COMPLETE_STATUS; // Lock all instances using the most DB-agnostic method
resultSet = stmt.executeQuery("select " + STATE_TABLE stmt.execute("UPDATE "+STATE_TABLE+" SET stateId=stateId");
+ ".stateId from " + STATE_TABLE + " where " String stateIn = "SELECT stateId FROM "+STATE_TABLE+" WHERE status='"+StateDBO.COMPLETE_STATUS+"'";
+ STATE_TABLE + ".status='" + status + "'"); for (int i = 0; i < PERSIST_TABLES_TO_CLEAR.length; i++) {
stmt.execute(
int i = 0; "DELETE FROM "+PERSIST_TABLES_TO_CLEAR[i]+
int totalCount = 0; " WHERE stateId IN (SELECT ehid FROM "+EVENT_HDLR_TABLE+
LOGGER.log(Level.INFO, I18n.loc("BPCOR-6152: Purging persistence data")); " WHERE stateId IN ("+stateIn+"))"
while (resultSet.next()) { );
String instanceId = resultSet.getString(1); stmt.execute("DELETE FROM "+PERSIST_TABLES_TO_CLEAR[i]+" WHERE stateId IN ("+stateIn+")");
purgeForAnInstance(instanceId, conn, false);
i++;
if ((PURGE_LOG_COUNT - 1) == i) {
LOGGER.log(Level.INFO, I18n.loc("BPCOR-6153: 50 instances data purged"));
totalCount = totalCount + i;
i = 1;
}
} }
totalCount = totalCount + i; stmt.execute("DELETE FROM "+EVENT_HDLR_TABLE+" WHERE stateId IN ("+stateIn+")");
LOGGER.log(Level.INFO, I18n.loc("BPCOR-6154: Done purging: Total {0} instances purged", totalCount)); int totalCount = stmt.executeUpdate("DELETE FROM "+STATE_TABLE+" WHERE status='"+StateDBO.COMPLETE_STATUS+"'");
conn.commit();
LOGGER.log(Level.INFO, I18n.loc("BPCOR-6154: Done purging: Total {0} instances purged", totalCount));
} catch (Exception e) { } catch (Exception e) {
// This could be due to the fact that the database connection is bad. Check for it and if so // This could be due to the fact that the database connection is bad. Check for it and if so
// mark it as bad. But do not attempt retries here. // mark it as bad. But do not attempt retries here.
connFac.validateNonXAConnection(dbConn); connFac.validateNonXAConnection(dbConn);
throw e; LOGGER.log(Level.WARNING, I18n.loc("BPCOR-6196: Exception while purging instances: {0}", e));
throw e;
} finally { } finally {
if (resultSet != null) { if (resultSet != null) {
try { try {
@ -135,76 +132,6 @@ public class PurgeData {
} }
} }
private void purgeForAnInstance(String instanceId, Connection conn, boolean isEH) throws Exception {
ResultSet resultSet = null;
Statement stmt = null;
try {
stmt = conn.createStatement();
String queryPart1 = "DELETE FROM ";
String queryPart2 = " WHERE stateid = '";
String queryPart3 = "'";
for (int i = 0; i < PERSIST_TABLES_TO_CLEAR.length; i++) {
String tableName = PERSIST_TABLES_TO_CLEAR[i];
StringBuilder queryBldr = new StringBuilder();
queryBldr.append(queryPart1);
queryBldr.append(tableName);
queryBldr.append(queryPart2);
queryBldr.append(instanceId);
queryBldr.append(queryPart3);
stmt.execute(queryBldr.toString());
}
resultSet = stmt.executeQuery("select " + EVENT_HDLR_TABLE
+ ".ehid from " + EVENT_HDLR_TABLE + " where "
+ EVENT_HDLR_TABLE + ".stateid='" + instanceId + "'");
while (resultSet.next()) {
String ehId = resultSet.getString(1);
purgeForAnInstance(ehId, conn, true);
}
// delete the event handler
StringBuilder queryBldr = new StringBuilder();
queryBldr.append(queryPart1);
queryBldr.append(EVENT_HDLR_TABLE);
queryBldr.append(queryPart2);
queryBldr.append(instanceId);
queryBldr.append(queryPart3);
stmt.execute(queryBldr.toString());
if (!isEH) {
// finally delete the entry from the state table.
queryBldr = new StringBuilder();
queryBldr.append(queryPart1);
queryBldr.append(STATE_TABLE);
queryBldr.append(queryPart2);
queryBldr.append(instanceId);
queryBldr.append(queryPart3);
stmt.execute(queryBldr.toString());
}
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException resCloseExcp) {
LOGGER.log(Level.WARNING, I18n.loc("BPCOR-6061: Exception occured while closing a JDBC Resultset"),
resCloseExcp);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException stmtCloseExcp) {
LOGGER.log(Level.WARNING, I18n.loc("BPCOR-6065: Exception while closing a JDBC statement"),
stmtCloseExcp);
}
}
}
}
public void purgeMonitoringData(DBConnectionFactory connFac) throws Exception { public void purgeMonitoringData(DBConnectionFactory connFac) throws Exception {
ResultSet resultSet = null; ResultSet resultSet = null;

View File

@ -647,6 +647,9 @@ BPCOR-6194 = encountered exception\: {0} while invoking\: {1} on class\: {2}
# com.sun.jbi.engine.bpel.core.bpel.management.BPELSEManagement # com.sun.jbi.engine.bpel.core.bpel.management.BPELSEManagement
BPCOR-6195 = bpel process {0} is not registered with the BPEL SE. No values were updated. BPCOR-6195 = bpel process {0} is not registered with the BPEL SE. No values were updated.
# com.sun.jbi.engine.bpel.core.bpel.persist.PurgeData
BPCOR-6196 = Exception while purging instances\: {0}
# com.sun.jbi.engine.bpel.core.bpel.connection.DBConnectionFactory # com.sun.jbi.engine.bpel.core.bpel.connection.DBConnectionFactory
BPCOR-7001 = Exception while looking up BPELSE Persistence Schema JDBC Non-XA Resource with the JNDI name {0}. Exception Detals \: {1} BPCOR-7001 = Exception while looking up BPELSE Persistence Schema JDBC Non-XA Resource with the JNDI name {0}. Exception Detals \: {1}

View File

@ -92,6 +92,7 @@
<!-- jbi service engines:: --> <!-- jbi service engines:: -->
<module>bpelse</module> <module>bpelse</module>
<module>bpelmonitortool</module>
<module>aspectse</module> <module>aspectse</module>
<module>dtelse</module> <module>dtelse</module>
<module>edmse</module> <module>edmse</module>
@ -115,7 +116,6 @@
--> -->
<!-- non components: --> <!-- non components: -->
<module>bpelmonitortool</module>
<module>cam</module> <module>cam</module>
</modules> </modules>
</profile> </profile>