📄 t_xa.java
字号:
/* Derby - Class org.apache.derbyTesting.unitTests.store.T_XA Copyright 1999, 2005 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 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 org.apache.derbyTesting.unitTests.store;import org.apache.derbyTesting.unitTests.harness.T_Generic;import org.apache.derbyTesting.unitTests.harness.T_Fail;import org.apache.derby.iapi.store.access.xa.*;import org.apache.derby.iapi.store.access.*;import org.apache.derby.iapi.services.io.FormatableBitSet;import org.apache.derby.iapi.reference.Property;import org.apache.derby.iapi.services.context.ContextService;import org.apache.derby.iapi.services.context.ContextManager;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.io.FormatIdUtil;import org.apache.derby.iapi.error.StandardException; import java.util.Properties; import javax.transaction.xa.XAResource;import javax.transaction.xa.Xid;public class T_XA extends T_Generic{ private static final String testService = "XaTest"; byte[] global_id = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 44, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}; byte[] branch_id = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 44, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}; AccessFactory store = null; public T_XA() { super(); } /* ** Methods of UnitTest. */ /* ** Methods required by T_Generic */ public String getModuleToTestProtocolName() { return AccessFactory.MODULE; } /** @exception T_Fail Unexpected behaviour from the API */ protected void runTests() throws T_Fail { // Create a AccessFactory to test. // don't automatic boot this service if it gets left around if (startParams == null) { startParams = new Properties(); } startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString()); // remove the service directory to ensure a clean run startParams.put(Property.DELETE_ON_CREATE, Boolean.TRUE.toString()); // see if we are testing encryption startParams = T_Util.setEncryptionParam(startParams); try { store = (AccessFactory) Monitor.createPersistentService( getModuleToTestProtocolName(), testService, startParams); } catch (StandardException mse) { throw T_Fail.exceptionFail(mse); } if (store == null) { throw T_Fail.testFailMsg( getModuleToTestProtocolName() + " service not started."); } REPORT("(unitTestMain) Testing " + testService); try { XATest_1(new commit_method(store, true)); XATest_2(new commit_method(store, true)); XATest_3(new commit_method(store, true)); XATest_4(new commit_method(store, true)); XATest_5(new commit_method(store, true)); XATest_6(new commit_method(store, true)); XATest_1(new commit_method(store, false)); XATest_2(new commit_method(store, false)); XATest_3(new commit_method(store, false)); XATest_4(new commit_method(store, false)); XATest_5(new commit_method(store, false)); XATest_6(new commit_method(store, false)); } catch (StandardException e) { String msg = e.getMessage(); if (msg == null) msg = e.getClass().getName(); REPORT(msg); e.printStackTrace(); throw T_Fail.exceptionFail(e); } catch (Throwable t) { t.printStackTrace(); } } /************************************************************************** * Utility methods. ************************************************************************** */ /************************************************************************** * Test Cases. ************************************************************************** */ /** * one phase commit xa transaction. * <p> * @exception StandardException Standard exception policy. **/ void XATest_1( commit_method commit_method) throws StandardException, T_Fail { REPORT("(XATest_1) starting"); ContextManager cm = ContextService.getFactory().getCurrentContextManager(); // COMMIT AN IDLE TRANSACTION. // Start a global transaction XATransactionController xa_tc = (XATransactionController) store.startXATransaction( cm, 42, // fake format id global_id, branch_id); // commit an idle transaction - using onePhase optimization. commit_method.commit(true, 42, global_id, branch_id, xa_tc); // done with this xact. xa_tc.destroy(); // COMMIT AN UPDATE ONLY TRANSACTION. // Start a global transaction xa_tc = (XATransactionController) store.startXATransaction( cm, 42, // fake format id global_id, branch_id); // Create a heap conglomerate. T_AccessRow template_row = new T_AccessRow(1); long conglomid = xa_tc.createConglomerate( "heap", // create a heap conglomerate template_row.getRowArray(), // 1 column template. null, //column sort order - not required for heap null, // default properties TransactionController.IS_DEFAULT); // not temporary // commit an idle transaction - using onePhase optimization. commit_method.commit(true, 42, global_id, branch_id, xa_tc); // done with this xact. xa_tc.destroy(); // COMMIT A READ ONLY TRANSACTION. // Start a global transaction xa_tc = (XATransactionController) store.startXATransaction( cm, 42, // fake format id global_id, branch_id); // Open a scan on the conglomerate. ScanController scan1 = xa_tc.openScan( conglomid, false, // don't hold 0, // not for update TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, (FormatableBitSet) null, // all columns, all as objects null, // start position - first row in conglomerate 0, // unused if start position is null. null, // qualifier - accept all rows null, // stop position - last row in conglomerate 0); // unused if stop position is null. scan1.next(); scan1.close(); // commit an idle transaction - using onePhase optimization. commit_method.commit(true, 42, global_id, branch_id, xa_tc); // done with this xact. xa_tc.destroy(); REPORT("(XATest_1) finishing"); } /** * simple two phase commit xa transaction. * <p> * @exception StandardException Standard exception policy. **/ void XATest_2( commit_method commit_method) throws StandardException, T_Fail { REPORT("(XATest_2) starting"); ContextManager cm = ContextService.getFactory().getCurrentContextManager(); // COMMIT AN IDLE TRANSACTION. // Start a global transaction XATransactionController xa_tc = (XATransactionController) store.startXATransaction( cm, 42, // fake format id global_id, branch_id); if (!xa_tc.isGlobal()) { throw T_Fail.testFailMsg("should be a global transaction."); } // This prepare will commit the idle transaction. if (xa_tc.xa_prepare() != XATransactionController.XA_RDONLY) { throw T_Fail.testFailMsg( "prepare of idle xact did not return XA_RDONLY."); } // commit an idle transaction - using onePhase optimization. try { // this should fail as the xact has been committed, so committing // it in 2 phase mode should fail. This test can't be run in // offline mode, no transaction will be found. Pass null as // global_id to make that test not run. commit_method.commit(false, 42, null, null, xa_tc); throw T_Fail.testFailMsg( "A XA_RDONLY prepare-committed xact cant be 2P xa_committed."); } catch (StandardException se) { // expected exception - drop through. } // should not be able to find this global xact, it has been committed if (((XAResourceManager) store.getXAResourceManager()).find( new XAXactId(42, global_id, branch_id)) != null) { throw T_Fail.testFailMsg( "A XA_RDONLY prepare-committed xact should not be findable."); } // done with this xact. xa_tc.destroy(); // COMMIT AN UPDATE ONLY TRANSACTION. // Start a global transaction xa_tc = (XATransactionController) store.startXATransaction( cm, 42, // fake format id global_id, branch_id); // Create a heap conglomerate. T_AccessRow template_row = new T_AccessRow(1); long conglomid = xa_tc.createConglomerate( "heap", // create a heap conglomerate template_row.getRowArray(), // 1 column template. null, //column sort order - not required for heap null, // default properties TransactionController.IS_DEFAULT); // not temporary // prepare the update xact. if (xa_tc.xa_prepare() != XATransactionController.XA_OK) { throw T_Fail.testFailMsg( "prepare of update xact did not return XA_OK."); } // commit an idle transaction - using onePhase optimization. commit_method.commit(false, 42, global_id, branch_id, xa_tc); // done with this xact. xa_tc.destroy(); // COMMIT A READ ONLY TRANSACTION. // Start a global transaction xa_tc = (XATransactionController) store.startXATransaction( cm, 42, // fake format id global_id, branch_id); // Open a scan on the conglomerate. ScanController scan1 = xa_tc.openScan( conglomid, false, // don't hold 0, // not for update TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, (FormatableBitSet) null, // all columns, all as objects null, // start position - first row in conglomerate 0, // unused if start position is null. null, // qualifier - accept all rows null, // stop position - last row in conglomerate 0); // unused if stop position is null. scan1.next(); scan1.close(); // This prepare will commit the idle transaction. if (xa_tc.xa_prepare() != XATransactionController.XA_RDONLY) { throw T_Fail.testFailMsg( "prepare of idle xact did not return XA_RDONLY."); } // commit an idle transaction - using onePhase optimization. try { // this should fail as the xact has been committed, so committing // it in 2 phase mode should fail. This test can't be run in // offline mode, no transaction will be found. Pass null as // global_id to make that test not run. commit_method.commit(false, 42, null, null, xa_tc); throw T_Fail.testFailMsg( "A XA_RDONLY prepare-committed xact cant be 2P xa_committed."); } catch (StandardException se) { // expected exception - drop through. } // should not be able to find this global xact, it has been committed if (((XAResourceManager) store.getXAResourceManager()).find( new XAXactId(42, global_id, branch_id)) != null) { throw T_Fail.testFailMsg( "A XA_RDONLY prepare-committed xact should not be findable."); } // done with this xact. xa_tc.destroy(); REPORT("(XATest_2) finishing"); } /** * Test aborts of unprepared xa transaction.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -