本文整理汇总了Java中javax.transaction.UserTransaction.begin方法的典型用法代码示例。如果您正苦于以下问题:Java UserTransaction.begin方法的具体用法?Java UserTransaction.begin怎么用?Java UserTransaction.begin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.transaction.UserTransaction
的用法示例。
在下文中一共展示了UserTransaction.begin方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRemoteXML
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
@Test
@RunAsClient
public void testRemoteXML() throws Exception {
logger.info("starting remoting ejb client test");
try {
createInitialContext();
String hostname = getLocalHost().getHostName().toLowerCase();
final UserTransaction userTransaction = getUserTransaction(hostname);
XMLRemote bean = lookup(XMLRemote.class, "bank");
assertEquals(STATUS_NO_TRANSACTION, bean.transactionStatus());
try {
userTransaction.begin();
bean.transactionStatus();
fail("the transaction is not supported");
} catch (EJBException | IllegalStateException e) {
logger.info("the transaction is not supported");
}
} finally {
closeContext();
}
}
示例2: test8CommitAfterTimeOut
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
@Test
public void test8CommitAfterTimeOut() throws Exception {
Context ctx = cache.getJNDIContext();
DataSource ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
ds2.getConnection();
GemFireTransactionDataSource ds =
(GemFireTransactionDataSource) ctx.lookup("java:/XAPooledDataSource");
UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
utx.begin();
Connection conn = ds.getConnection();
String sql = "create table newTable2 (id integer)";
Statement sm = conn.createStatement();
sm.execute(sql);
utx.setTransactionTimeout(30);
sql = "insert into newTable2 values (1)";
sm.execute(sql);
sql = "select * from newTable2 where id = 1";
ResultSet rs = sm.executeQuery(sql);
if (!rs.next()) {
fail("Transaction not committed");
}
sql = "drop table newTable2";
sm.execute(sql);
sm.close();
conn.close();
utx.setTransactionTimeout(1);
Thread.sleep(3000);
try {
utx.commit();
fail("exception did not occur on commit although transaction timed out");
} catch (Exception expected) {
}
}
示例3: testRollback
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
public void testRollback() throws Throwable
{
UserTransaction txn = transactionService.getUserTransaction();
try
{
txn.begin();
singleton.put(INTEGER_TWO);
check(INTEGER_TWO, true);
check(null, false);
// rollback
txn.rollback();
}
catch (Throwable e)
{
try { txn.rollback(); } catch (Throwable ee) {}
throw e;
}
check(null, true);
check(null, false);
}
示例4: startUserTransaction
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
/**
* If <em>wrapInUserTransaction</em> is true, starts a new UserTransaction
* and returns it. Otherwise, or if establishing the transaction fail, it
* will return null.
*/
private UserTransaction startUserTransaction() {
if (wrapInUserTransaction == false) {
return null;
}
UserTransaction userTransaction = null;
try {
userTransaction = UserTransactionHelper.lookupUserTransaction();
userTransaction.begin();
} catch (Throwable t) {
UserTransactionHelper.returnUserTransaction(userTransaction);
userTransaction = null;
getLog().error("Failed to start UserTransaction for plugin: " + getName(), t);
}
return userTransaction;
}
示例5: testLazyLoadIssue
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
/**
* Deletes a child node and then iterates over the children of the parent node,
* getting the QName. This caused some issues after we did some optimization
* using lazy loading of the associations.
*/
public void testLazyLoadIssue() throws Exception
{
Map<QName, ChildAssociationRef> assocRefs = buildNodeGraph();
// commit results
setComplete();
endTransaction();
UserTransaction userTransaction = txnService.getUserTransaction();
try
{
userTransaction.begin();
ChildAssociationRef n6pn8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8"));
NodeRef n6Ref = n6pn8Ref.getParentRef();
NodeRef n8Ref = n6pn8Ref.getChildRef();
// delete n8
nodeService.deleteNode(n8Ref);
// get the parent children
List<ChildAssociationRef> assocs = nodeService.getChildAssocs(n6Ref);
for (ChildAssociationRef assoc : assocs)
{
// just checking
}
userTransaction.commit();
}
catch(Exception e)
{
try { userTransaction.rollback(); } catch (IllegalStateException ee) {}
throw e;
}
}
示例6: testReplicationExecutionLocking
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
/**
* Check that the locking works.
* Take a 10 second lock on the job, then execute.
* Ensure that we really wait a little over 10 seconds.
*/
public void testReplicationExecutionLocking() throws Exception
{
// We need the test transfer target for this test
makeTransferTarget();
// Create a task
ReplicationDefinition rd = replicationService.createReplicationDefinition(ACTION_NAME, "Test");
rd.setTargetName(TRANSFER_TARGET);
rd.getPayload().add(folder1);
rd.getPayload().add(folder2);
// Get the lock, and run
long start = System.currentTimeMillis();
String token = jobLockService.getLock(
rd.getReplicationQName(),
10 * 1000,
1,
1
);
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
try {
actionService.executeAction(rd, replicationRoot);
} catch(ReplicationServiceException e) {
// This shouldn't happen normally! Something is wrong!
// Tidy up before we throw the exception
txn.rollback();
throw e;
}
txn.commit();
long end = System.currentTimeMillis();
assertTrue(
"Should wait for the lock, but didn't (waited " +
((end-start)/1000.0) + " seconds, not 10)",
end-start > 10000
);
}
示例7: test6CommitBeforeTimeOut
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
@Test
public void test6CommitBeforeTimeOut() throws Exception {
UserTransaction utx = new UserTransactionImpl();
utx.begin();
utx.setTransactionTimeout(6);
Thread.sleep(2000);
utx.commit();
}
示例8: tearDown
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
@Override
protected void tearDown() throws Exception
{
super.tearDown();
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
personManager.clearPeople();
// Zap any replication definitions we created
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
for(ReplicationDefinition rd : replicationService.loadReplicationDefinitions()) {
replicationService.deleteReplicationDefinition(rd);
}
AuthenticationUtil.clearCurrentSecurityContext();
// Clear out the running actions
for(ExecutionSummary es : actionTrackingService.getAllExecutingActions())
{
executingActionsCache.remove(
AbstractActionWebscript.getRunningId(es)
);
}
txn.commit();
}
示例9: setup
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
@Before
public void setup() throws SystemException, NotSupportedException
{
transactionService = (TransactionService)ctx.getBean("transactionService");
keyStoreChecker = (KeyStoreChecker)ctx.getBean("keyStoreChecker");
encryptionKeysRegistry = (EncryptionKeysRegistry)ctx.getBean("encryptionKeysRegistry");
keyResourceLoader = (KeyResourceLoader)ctx.getBean("springKeyResourceLoader");
backupEncryptor = (DefaultEncryptor)ctx.getBean("backupEncryptor");
toDelete = new ArrayList<String>(10);
AuthenticationUtil.setRunAsUserSystem();
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
}
示例10: testGetReaderWriter
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
public void testGetReaderWriter() throws Exception
{
// testing a failure
txn.commit();
txn = transactionService.getUserTransaction();
txn.begin();
FileInfo dirInfo = getByName(NAME_L0_FOLDER_A, true);
UserTransaction rollbackTxn = null;
try
{
rollbackTxn = transactionService.getNonPropagatingUserTransaction();
rollbackTxn.begin();
fileFolderService.getWriter(dirInfo.getNodeRef());
fail("Failed to detect content write to folder");
}
catch (RuntimeException e)
{
// expected
}
finally
{
rollbackTxn.rollback();
}
FileInfo fileInfo = getByName(NAME_L1_FILE_A, false);
ContentWriter writer = fileFolderService.getWriter(fileInfo.getNodeRef());
assertNotNull("Writer is null", writer);
// write some content
String content = "ABC";
writer.putContent(content);
// read the content
ContentReader reader = fileFolderService.getReader(fileInfo.getNodeRef());
assertNotNull("Reader is null", reader);
String checkContent = reader.getContentString();
assertEquals("Content mismatch", content, checkContent);
}
示例11: getTrx
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
public void getTrx() throws Exception
{
UserTransaction txn = getTxn();
txn.begin();
txn = null;
}
示例12: testJavascriptAPI
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
public void testJavascriptAPI() throws Exception
{
// We need a background action to sleep for long enough for
// it still to be running when the JS fires off
final SleepActionExecuter sleepActionExec = (SleepActionExecuter) ctx.getBean(SleepActionExecuter.NAME);
sleepActionExec.setSleepMs(2000);
ActionImpl sleepAction;
ActionImpl action;
// Create three test actions:
((ActionTrackingServiceImpl) actionTrackingService).resetNextExecutionId();
// Sleep one that will still be running
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
sleepAction = (ActionImpl) createWorkingSleepAction(null);
sleepAction.setNodeRef(nodeRef); // This isn't true!
this.actionService.executeAction(sleepAction, null, false, true);
txn.commit();
// Move one that will appear to be "running"
action = (ActionImpl) createFailingMoveAction();
actionTrackingService.recordActionExecuting(action);
// Finally one that has "failed"
// (Shouldn't show up in any lists)
txn = transactionService.getUserTransaction();
txn.begin();
action = (ActionImpl) createWorkingSleepAction(null);
action.setExecutionStartDate(new Date(1234));
action.setExecutionEndDate(new Date(54321));
action.setExecutionStatus(ActionStatus.Failed);
this.actionService.saveAction(this.nodeRef, action);
txn.commit();
// Call the test
Map<String, Object> model = new HashMap<String, Object>();
model.put("NodeRef", nodeRef.toString());
model.put("SleepAction", sleepAction);
ScriptLocation location = new ClasspathScriptLocation(
"org/alfresco/repo/action/script/test_actionTrackingService.js");
this.scriptService.executeScript(location, model);
}
示例13: testWorkingActions
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
/** Working actions go into the cache, then out */
public void testWorkingActions() throws Exception
{
final SleepActionExecuter sleepActionExec =
(SleepActionExecuter)ctx.getBean(SleepActionExecuter.NAME);
sleepActionExec.resetTimesExecuted();
sleepActionExec.setSleepMs(10000);
// Have it run asynchronously
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
Action action = createWorkingSleepAction("54321");
assertNull(action.getExecutionStartDate());
assertNull(action.getExecutionEndDate());
assertNull(action.getExecutionFailureMessage());
assertEquals(ActionStatus.New, action.getExecutionStatus());
String key = ActionTrackingServiceImpl.generateCacheKey(action);
assertEquals(null, executingActionsCache.get(key));
this.actionService.executeAction(action, this.nodeRef, false, true);
// End the transaction. Should allow the async action
// to start up, and begin sleeping
txn.commit();
Thread.sleep(150);
// The action should now be running
// It will have got an execution instance id, so a new key
key = ActionTrackingServiceImpl.generateCacheKey(action);
// Check it's in the cache
System.out.println("Checking the cache for " + key);
assertNotNull(executingActionsCache.get(key));
ExecutionSummary s = ActionTrackingServiceImpl.buildExecutionSummary(action);
ExecutionDetails d = actionTrackingService.getExecutionDetails(s);
assertNotNull(d.getExecutionSummary());
assertEquals("sleep-action", d.getActionType());
assertEquals("54321", d.getActionId());
assertEquals(1, d.getExecutionInstance());
assertEquals(null, d.getPersistedActionRef());
assertNotNull(null, d.getStartedAt());
// Tell it to stop sleeping
// Then wait for it to finish
asyncOccurs.awaitExecution(null, sleepActionExec.getExecutingThread(), action.getActionDefinitionName());
// Ensure it went away again
assertEquals(ActionStatus.Completed, action.getExecutionStatus());
assertEquals(null, executingActionsCache.get(key));
d = actionTrackingService.getExecutionDetails(s);
assertEquals(null, d);
}
示例14: testTransactionId
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
public void testTransactionId() throws Exception
{
// get a user transaction
TransactionService transactionService = serviceRegistry.getTransactionService();
UserTransaction txn = transactionService.getUserTransaction();
assertNull("Thread shouldn't have a txn ID", AlfrescoTransactionSupport.getTransactionId());
assertEquals("No transaction start time expected", -1, AlfrescoTransactionSupport.getTransactionStartTime());
// begin the txn
txn.begin();
String txnId = AlfrescoTransactionSupport.getTransactionId();
assertNotNull("Expected thread to have a txn id", txnId);
long txnStartTime = AlfrescoTransactionSupport.getTransactionStartTime();
assertTrue("Expected a transaction start time", txnStartTime > 0);
// check that the txn id and time doesn't change
String txnIdCheck = AlfrescoTransactionSupport.getTransactionId();
assertEquals("Transaction ID changed on same thread", txnId, txnIdCheck);
long txnStartTimeCheck = AlfrescoTransactionSupport.getTransactionStartTime();
assertEquals("Transaction start time changed on same thread", txnStartTime, txnStartTimeCheck);
// begin a new, inner transaction
{
UserTransaction txnInner = transactionService.getNonPropagatingUserTransaction();
String txnIdInner = AlfrescoTransactionSupport.getTransactionId();
assertEquals("Inner transaction not started, so txn ID should not change", txnId, txnIdInner);
long txnStartTimeInner = AlfrescoTransactionSupport.getTransactionStartTime();
assertEquals("Inner transaction not started, so txn start time should not change", txnStartTime, txnStartTimeInner);
// begin the nested txn
txnInner.begin();
// check the ID for the outer transaction
txnIdInner = AlfrescoTransactionSupport.getTransactionId();
assertNotSame("Inner txn ID must be different from outer txn ID", txnIdInner, txnId);
// Check the time against the outer transaction
txnStartTimeInner = AlfrescoTransactionSupport.getTransactionStartTime();
assertTrue(
"Inner transaction start time should be greater or equal (accuracy) to the outer's",
txnStartTime <= txnStartTimeInner);
// rollback the nested txn
txnInner.rollback();
txnIdCheck = AlfrescoTransactionSupport.getTransactionId();
assertEquals("Txn ID not popped inner txn completion", txnId, txnIdCheck);
}
// rollback
txn.rollback();
assertNull("Thread shouldn't have a txn ID after rollback", AlfrescoTransactionSupport.getTransactionId());
// start a new transaction
txn = transactionService.getUserTransaction();
txn.begin();
txnIdCheck = AlfrescoTransactionSupport.getTransactionId();
assertNotSame("New transaction has same ID", txnId, txnIdCheck);
// rollback
txn.rollback();
assertNull("Thread shouldn't have a txn ID after rollback", AlfrescoTransactionSupport.getTransactionId());
}
示例15: testListenerNew
import javax.transaction.UserTransaction; //导入方法依赖的package包/类
public void testListenerNew() throws Exception
{
final List<String> strings = new ArrayList<String>(1);
// anonymous inner class to test it
org.alfresco.util.transaction.TransactionListener listener = new org.alfresco.util.transaction.TransactionListener()
{
@Override
public void beforeCommit(boolean readOnly)
{
strings.add("beforeCommit");
}
@Override
public void beforeCompletion()
{
strings.add("beforeCompletion");
}
@Override
public void afterCommit()
{
strings.add("afterCommit");
}
@Override
public void afterRollback()
{
strings.add("afterRollback");
}
};
// begin a transaction
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
// register it
AlfrescoTransactionSupport.bindListener(listener);
// test commit
txn.commit();
assertTrue("beforeCommit not called on listener", strings.contains("beforeCommit"));
assertTrue("beforeCompletion not called on listener", strings.contains("beforeCompletion"));
assertTrue("afterCommit not called on listener", strings.contains("afterCommit"));
}