org.springframework.transaction.jta.JtaTransactionObject Java Examples

The following examples show how to use org.springframework.transaction.jta.JtaTransactionObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataSourceJtaTransactionTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testJtaTransactionWithConnectionHolderStillBound() throws Exception {
	@SuppressWarnings("serial")
	JtaTransactionManager ptm = new JtaTransactionManager(userTransaction) {

		@Override
		protected void doRegisterAfterCompletionWithJtaTransaction(
				JtaTransactionObject txObject,
				final List<TransactionSynchronization> synchronizations)
				throws RollbackException, SystemException {
			Thread async = new Thread() {
				@Override
				public void run() {
					invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_COMMITTED);
				}
			};
			async.start();
			try {
				async.join();
			}
			catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}
	};
	TransactionTemplate tt = new TransactionTemplate(ptm);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
	assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

	given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE);
	for (int i = 0; i < 3; i++) {
		final boolean releaseCon = (i != 1);

		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
				assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
				assertTrue("Is existing transaction", !status.isNewTransaction());

				Connection c = DataSourceUtils.getConnection(dataSource);
				assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource));
				DataSourceUtils.releaseConnection(c, dataSource);

				c = DataSourceUtils.getConnection(dataSource);
				assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource));
				if (releaseCon) {
					DataSourceUtils.releaseConnection(c, dataSource);
				}
			}
		});

		if (!releaseCon) {
			assertTrue("Still has connection holder", TransactionSynchronizationManager.hasResource(dataSource));
		}
		else {
			assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
		}
		assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
	}
	verify(connection, times(3)).close();
}
 
Example #2
Source File: DataSourceJtaTransactionTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testJtaTransactionWithConnectionHolderStillBound() throws Exception {
	@SuppressWarnings("serial")
	JtaTransactionManager ptm = new JtaTransactionManager(userTransaction) {

		@Override
		protected void doRegisterAfterCompletionWithJtaTransaction(
				JtaTransactionObject txObject,
				final List<TransactionSynchronization> synchronizations)
				throws RollbackException, SystemException {
			Thread async = new Thread() {
				@Override
				public void run() {
					invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_COMMITTED);
				}
			};
			async.start();
			try {
				async.join();
			}
			catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}
	};
	TransactionTemplate tt = new TransactionTemplate(ptm);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
	assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

	given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE);
	for (int i = 0; i < 3; i++) {
		final boolean releaseCon = (i != 1);

		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
				assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
				assertTrue("Is existing transaction", !status.isNewTransaction());

				Connection c = DataSourceUtils.getConnection(dataSource);
				assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource));
				DataSourceUtils.releaseConnection(c, dataSource);

				c = DataSourceUtils.getConnection(dataSource);
				assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource));
				if (releaseCon) {
					DataSourceUtils.releaseConnection(c, dataSource);
				}
			}
		});

		if (!releaseCon) {
			assertTrue("Still has connection holder", TransactionSynchronizationManager.hasResource(dataSource));
		}
		else {
			assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
		}
		assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
	}
	verify(connection, times(3)).close();
}
 
Example #3
Source File: DataSourceJtaTransactionTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testJtaTransactionWithConnectionHolderStillBound() throws Exception {
	@SuppressWarnings("serial")
	JtaTransactionManager ptm = new JtaTransactionManager(userTransaction) {

		@Override
		protected void doRegisterAfterCompletionWithJtaTransaction(
				JtaTransactionObject txObject,
				final List<TransactionSynchronization> synchronizations)
				throws RollbackException, SystemException {
			Thread async = new Thread() {
				@Override
				public void run() {
					invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_COMMITTED);
				}
			};
			async.start();
			try {
				async.join();
			}
			catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}
	};
	TransactionTemplate tt = new TransactionTemplate(ptm);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
	assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

	given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE);
	for (int i = 0; i < 3; i++) {
		final boolean releaseCon = (i != 1);

		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
				assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
				assertTrue("Is existing transaction", !status.isNewTransaction());

				Connection c = DataSourceUtils.getConnection(dataSource);
				assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource));
				DataSourceUtils.releaseConnection(c, dataSource);

				c = DataSourceUtils.getConnection(dataSource);
				assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource));
				if (releaseCon) {
					DataSourceUtils.releaseConnection(c, dataSource);
				}
			}
		});

		if (!releaseCon) {
			assertTrue("Still has connection holder", TransactionSynchronizationManager.hasResource(dataSource));
		}
		else {
			assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
		}
		assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
	}
	verify(connection, times(3)).close();
}
 
Example #4
Source File: DataSourceJtaTransactionTests.java    From effectivejava with Apache License 2.0 4 votes vote down vote up
@Test
public void testJtaTransactionWithConnectionHolderStillBound() throws Exception {
	@SuppressWarnings("serial")
	JtaTransactionManager ptm = new JtaTransactionManager(userTransaction) {

		@Override
		protected void doRegisterAfterCompletionWithJtaTransaction(
				JtaTransactionObject txObject,
				final List<TransactionSynchronization> synchronizations)
				throws RollbackException, SystemException {
			Thread async = new Thread() {
				@Override
				public void run() {
					invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_COMMITTED);
				}
			};
			async.start();
			try {
				async.join();
			}
			catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}
	};
	TransactionTemplate tt = new TransactionTemplate(ptm);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
	assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

	given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE);
	for (int i = 0; i < 3; i++) {
		final boolean releaseCon = (i != 1);

		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
				assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
				assertTrue("Is existing transaction", !status.isNewTransaction());

				Connection c = DataSourceUtils.getConnection(dataSource);
				assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource));
				DataSourceUtils.releaseConnection(c, dataSource);

				c = DataSourceUtils.getConnection(dataSource);
				assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource));
				if (releaseCon) {
					DataSourceUtils.releaseConnection(c, dataSource);
				}
			}
		});

		if (!releaseCon) {
			assertTrue("Still has connection holder", TransactionSynchronizationManager.hasResource(dataSource));
		}
		else {
			assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
		}
		assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
	}
	verify(connection, times(3)).close();
}