Atomicity原子事务在产品中的应用
1. Atomicity简介
Atomic is one of the four properties defined in acronym that defines that a transaction must execute completely or not at all. This means atomicity guarantees that the operations with in a transaction work on the all-or-nothing paradigm means either all the database updates are performed or nothing happens whenever an error occurs.
TS can enable atomicity feature for test purpose. When atomicity feature is enabled and one or more specified Error/Exception code point is enabled as well, program can throw NullPointerException or DataBaseException proactively.
For example, in TS system, when an event is capture, TS should save it in database, but if there are errors or exception throws when processing which does not handled by system, then the event should not saved successfully. Database is not changed. So that is called an atomicity.
2. How atomicity implement in code
How atomicity triggered – describe from code level. At step that needs to be viewed of a complete transaction, atomicity code is inserted like below. It calls throwXXX() method directly from TestExceptionUtility class, which parameter is an meaningful verbal sign.
…….
UserGroup newGroup = (UserGroup)policy.getUserGroup();
TestExceptionUtility.throwNullPointerException(“SPEBeforeDropUserGroupView”);
dropUserGroupDBViews(newGroup, con);
TestExceptionUtility.throwNullPointerException(“QSMAfterDropUserGroupView”);
……..
TestExceptionUtility.java是核心的类
public class TestExceptionUtility { public static final String copyright = "(c) Copyright IBM Corporation 2006."; private static final Logger logger = Logger.getLogger(TestExceptionUtility.class); public static final String enableTestExceptionProperty = "TS.EXCEPTIONTEST"; public static final String TestExceptionPropertyDetailedLogging = "detailedLogging"; public static final String captureBeforeCommit = "captureBeforeCommit"; public static final String MDMXMLImportBeforeCommit = "MDMXMLImportBeforeCommit"; public static final String MDMReloadElement = "MDMReloadElement"; public static final String MDMInitialLoad = "MDMInitialLoad"; public static final String FailedEventPersistBeforeCommit = "FailedEventPersistBeforeCommit"; public static final String FailedEventQueryBeforeCommit = "FailedEventQueryBeforeCommit"; public static final String FailedEventDeleteBeforeCommit = "FailedEventDeleteBeforeCommit"; public static final String FailedEventDeleteListBeforeCommit = "FailedEventDeleteListBeforeCommit"; public static final String SNMCreatePolicyBeforeCommit = "SNMCreatePolicyBeforeCommit"; public static final String SNMImportPoliciesBeforeCommit = "SNMImportPoliciesBeforeCommit"; public static final String SNMCreateResourceBeforeCommit = "SNMCreateResourceBeforeCommit"; public static final String SNMImportResourcesBeforeCommit = "SNMImportResourcesBeforeCommit"; public static final String SNMCreateOrImportRuleBeforeCommit = "SNMCreateOrImportRuleBeforeCommit"; public static final String SNMSetRulePriortiesBeforeCommit = "SNMSetRulePriortiesBeforeCommit"; public static final String SNMCreateTagBeforeCommit = "SNMCreateTagBeforeCommit"; public static final String SNMConfirmTagsBeforeCommit = "SNMConfirmTagsBeforeCommit"; public static final String SNMReturnTagsBeforeCommit = "SNMReturnTagsBeforeCommit"; public static final String QSMBeforeRegisterUserCommits = "QSMBeforeRegisterUserCommit"; public static final String QSMAfterDropAllViews = "QSMAfterDropAllViews"; public static final String QSMAfterPolicySave = "QSMAfterPolicySave"; public static final String QSMBeforePoliciesCommit = "QSMBeforePoliciesCommit"; public static final String QSMAfterDropUserGroupView = "QSMAfterDropUserGroupView"; public static final String QSMBeforePolicyChangeCommit = "QSMBeforePolicyChangeCommit"; public static final String QSMAfterDeleteUserGroupFromUserRole = "QSMAfterDeleteUserGroupFromUserRole"; public static final String QSMBeforeCommitAfterAddUser = "QSMBeforeCommitAfterAddUser"; public static final String QSMBeforeCommitAfterAddUserOrganization = "QSMBeforeCommitAfterAddUserOrganization"; public static final String QSMReadsecurityDataAfterReadAuthRules = "QSMReadSecurityDataAfterReadAuthRules"; public static final String QSMBeforeGetSecurityPolicy = "QSMBeforeGetSecurityPolicy"; public static final String QSMEntityRuleBeforePSrun = "QSMEntityRuleBeforePSrun"; public static final String QuerySchedBeforeUpdateJobData = "QuerySchedBeforeUpdateJobData"; public static final String QuerySchedBeforeSelectSubscriberList = "QuerySchedBeforeSelectSubscriberList"; public static final String QuerySchedgetSubscrListAfterSubscrList = "QuerySchedgetSubscrListAfterSubscrList"; public static final String QuerySchedserializeBeforeUpdate = "QuerySchedserializeBeforeUpdate"; public static final String QuerySchedserializeBeforeCommit = "QuerySchedserializeBeforeCommit"; public static final String QuerySchedserializeBeforeRemoveUpdate = "QuerySchedserializeBeforeRemoveUpdate"; public static final String QuerySchedBeforeEventDelivertoDest = "QuerySchedBeforeEventDelivertoDest"; public static final String QuerySchedserializeBeforeFQInsertCommit = "QuerySchedserializeBeforeFQInsertCommit"; public static final String QuerySchedserializeBeforeFQRemoveCommit = "QuerySchedserializeBeforeFQRemoveCommit"; public static final String QSMViewBeforeDropViewStmt = "QSMViewBeforeDropViewStmt"; public static final String QSMViewBeforeCreateViewStmt = "QSMViewBeforeCreateViewStmt"; public static final String QGBeforeQueryRun1 = "QGBeforeQueryRun1"; public static final String QGBeforeQueryRun2 = "QGBeforeQueryRun2"; public static final String SchedBeforeCancelTask = "SchedBeforeCancelTask"; public static final String SchedBeforeCreateTask = "SchedBeforeCreateTask"; public static final String ClassLoaderRegAddJarBeforeCommit = "ClassLoaderRegAddJarBeforeCommit"; public static final String ClassLoaderRegRemoveJarBeforeCommit = "ClassLoaderRegRemoveJarBeforeCommit"; public static final String EventBrowserBeforeAddQueryCommit = "EventBrowserBeforeAddQueryCommit"; public static final String EventBrowserBeforeDeleteQueryCommit = "EventBrowserBeforeDeleteQueryCommit"; public static final String AlertsNotificationUIBeforeSelect = "AlertsNotificationUIBeforeSelect"; public static final String AlertsComponentDatabaseFailureBeforeAddingAlert = "AlertsComponentDatabaseFailureBeforeAddingAlert"; public static final String AlertsComponentDatabaseFailureBeforeDeletingAlert = "AlertsComponentDatabaseFailureBeforeDeletingAlert"; public static final String AlertsComponentDatabaseFailureBeforeEnablingOrDisablingAlert = "AlertsComponentDatabaseFailureBeforeEnablingOrDisablingAlert"; public static final String AlertsComponentDatabaseFailureBeforeAddingNotificationConfiguration = "AlertsComponentDatabaseFailureBeforeAddingNotificationConfiguration"; public static final String AlertsComponentDatabaseFailureBeforeAddingParameterValue = "AlertsComponentDatabaseFailureBeforeAddingParameterValue"; public static final String AlertsComponentDatabaseFailureBeforeDeletingParameterValue = "AlertsComponentDatabaseFailureBeforeDeletingParameterValue"; public static final String AlertsComponentDatabaseFailureBeforeAddingSubscription = "AlertsComponentDatabaseFailureBeforeAddingSubscription"; public static final String AlertsComponentDatabaseFailureBeforeEnablingOrDisablingSubscription = "AlertsComponentDatabaseFailureBeforeEnablingOrDisablingSubscription"; public static final String AlertsComponentDatabaseFailureBeforeUpdatingSubscription = "AlertsComponentDatabaseFailureBeforeUpdatingSubscription"; public static final String AlertsComponentDatabaseFailureBeforeDeletingSubscription = "AlertsComponentDatabaseFailureBeforeDeletingSubscription"; public static final String SPEBeforePolicyAddEditDelete = "SPEBeforePolicyAddEditDelete"; public static final String SPEBeforeSavingRules = "SPEBeforeSavingRules"; public static final String SPEBeforeInsertEntityRules = "SPEBeforeInsertEntityRules"; public static final String SPEOnImportBeforeDeleteOfPolicies = "SPEOnImportBeforeDeleteOfPolicies"; public static final String SPEBeforeDropUserGroupView = "SPEBeforeDropUserGroupView"; public static final String SPEBeforeInsertDBRules = "SPEBeforeInsertDBRules"; private static boolean exceptionsEnabled = false; private static Set enabledExceptions = new HashSet(); private static boolean detailedLoggingEnabled = false; private static Set enabledWaitpoints; private static int sleepSeconds; static { exceptionsEnabled = System.getProperty("TS.EXCEPTIONTEST") != null; if (exceptionsEnabled) { String propValue = System.getProperty("TS.EXCEPTIONTEST"); Object[] argsListForMessage = { "TS.EXCEPTIONTEST", propValue }; logger.warn(RFIDICMessages.getMessage(21396397, argsListForMessage).getIdAndMessage()); if (propValue.equals("detailedLogging")) { logger.warn(RFIDICMessages.getMessage(21396398).getIdAndMessage()); detailedLoggingEnabled = true; } } enabledWaitpoints = new HashSet(); sleepSeconds = 10; } public static void throwDatabaseException(String codePoint) throws DatabaseException { if (!(exceptionsEnabled)) return; boolean codepointEnabled = enabledExceptions.contains(codePoint); if (detailedLoggingEnabled) { Object[] argsListForMessage = { codePoint, new Boolean(codepointEnabled).toString() }; System.out.println(RFIDICMessages.getMessage(21396386, argsListForMessage).getIdAndMessage()); if (!(codepointEnabled)) { System.out.println(RFIDICMessages.getMessage(21396385, enabledExceptions.toString()).getIdAndMessage()); } } if (codepointEnabled) { String msg = RFIDICMessages.getMessage(21396393, codePoint).getIdAndMessage(); System.out.println(msg); throw new DatabaseException(msg); } checkWait(codePoint); } public static void throwNullPointerException(String codePoint) throws NullPointerException { if (!(exceptionsEnabled)) return; boolean codepointEnabled = enabledExceptions.contains(codePoint); if (detailedLoggingEnabled) { Object[] argsListForMessage = { codePoint, new Boolean(codepointEnabled).toString() }; String msg = RFIDICMessages.getMessage(21396394, argsListForMessage).getIdAndMessage(); System.out.println(msg); if (!(codepointEnabled)) { System.out.println(RFIDICMessages.getMessage(21396385, enabledExceptions.toString()).getIdAndMessage()); } } if (codepointEnabled) { String msg = "TestExceptionUtility " + codePoint + " throws exception"; System.out.println(msg); throw new NullPointerException(msg); } checkWait(codePoint); } private static void checkWait(String waitpoint) { if (!(enabledWaitpoints.contains(waitpoint))) return; while (true) try { Thread.sleep(sleepSeconds * 1000); if (!(enabledWaitpoints.contains(waitpoint))) { logger.warn(RFIDICMessages.getMessage(21396395, waitpoint).getIdAndMessage()); return; } logger.warn(RFIDICMessages.getMessage(21396391, waitpoint).getIdAndMessage()); } catch (InterruptedException e) { logger.warn(RFIDICMessages.getMessage(21396396, waitpoint).getIdAndMessage()); return; } } public static void setExceptionsEnabled(boolean enabled) { exceptionsEnabled = enabled; if (exceptionsEnabled) logger.warn(RFIDICMessages.getMessage(21396399).getIdAndMessage()); else logger.warn(RFIDICMessages.getMessage(21396388).getIdAndMessage()); } public static String enableException(String codepoint) { if (!(exceptionsEnabled)) { logger.error(RFIDICMessages.getMessage(21396388).getIdAndMessage()); } enabledExceptions.add(codepoint); String msg = RFIDICMessages.getMessage(21396401, codepoint).getIdAndMessage(); System.out.println(msg); return msg; } public static String clearException(String codepoint) { if (!(exceptionsEnabled)) { logger.error(RFIDICMessages.getMessage(21396388).getIdAndMessage()); } enabledExceptions.remove(codepoint); String msg = RFIDICMessages.getMessage(21396402, codepoint).getIdAndMessage(); System.out.println(msg); return msg; } public static String clearAllException() { if (!(exceptionsEnabled)) { logger.error(RFIDICMessages.getMessage(21396388).getIdAndMessage()); } enabledExceptions.clear(); String msg = RFIDICMessages.getMessage(21396392).getIdAndMessage(); System.out.println(msg); return msg; } public static String[] listExceptions() { if (!(exceptionsEnabled)) { logger.error(RFIDICMessages.getMessage(21396388).getIdAndMessage()); } String[] retArray = new String[enabledExceptions.size()]; System.arraycopy(enabledExceptions.toArray(), 0, retArray, 0, enabledExceptions.size()); String msg = RFIDICMessages.getMessage(21396385, enabledExceptions.toString()).getIdAndMessage(); System.out.println(msg); return retArray; } public static String enableWaitpoint(String waitpoint) { if (!(exceptionsEnabled)) { logger.error(RFIDICMessages.getMessage(21396388).getIdAndMessage()); } enabledWaitpoints.add(waitpoint); String msg = RFIDICMessages.getMessage(21396401, waitpoint).getIdAndMessage(); System.out.println(msg); return msg; } public static String clearWaitpoint(String waitpoint) { if (!(exceptionsEnabled)) { logger.error(RFIDICMessages.getMessage(21396388).getIdAndMessage()); } enabledWaitpoints.remove(waitpoint); String msg = RFIDICMessages.getMessage(21396400, waitpoint).getIdAndMessage(); System.out.println(msg); return msg; } public static String clearAllWaitpoints() { if (!(exceptionsEnabled)) { logger.error(RFIDICMessages.getMessage(21396388).getIdAndMessage()); } enabledWaitpoints.clear(); String msg = RFIDICMessages.getMessage(21396389).getIdAndMessage(); System.out.println(msg); return msg; } public static String[] listWaitpoint() { if (!(exceptionsEnabled)) { logger.error(RFIDICMessages.getMessage(21396388).getIdAndMessage()); } String[] retArray = new String[enabledWaitpoints.size()]; System.arraycopy(enabledWaitpoints.toArray(), 0, retArray, 0, enabledWaitpoints.size()); String msg = RFIDICMessages.getMessage(21396390, enabledWaitpoints.toString()).getIdAndMessage(); System.out.println(msg); return retArray; } public static void enableTestExceptionUtility() { exceptionsEnabled = true; } public static void disableTestExceptionUtility() { exceptionsEnabled = false; } public static void setSleepSeconds(int secs) { sleepSeconds = secs; } } |