Java Code Examples for javax.transaction.Synchronization#afterCompletion()

The following examples show how to use javax.transaction.Synchronization#afterCompletion() . 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: LobTypeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlobByteArrayTypeWithJtaSynchronizationAndRollback() throws Exception {
	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.getTransaction()).willReturn(transaction);

	byte[] content = "content".getBytes();
	given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(content);

	BlobByteArrayType type = new BlobByteArrayType(lobHandler, tm);
	assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
	type.nullSafeSet(ps, content, 1);
	Synchronization synch = transaction.getSynchronization();
	assertNotNull(synch);
	synch.afterCompletion(Status.STATUS_ROLLEDBACK);
	verify(lobCreator).setBlobAsBytes(ps, 1, content);
}
 
Example 2
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlobByteArrayTypeWithJtaSynchronization() throws Exception {
	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.getTransaction()).willReturn(transaction);

	byte[] content = "content".getBytes();
	given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(content);

	BlobByteArrayType type = new BlobByteArrayType(lobHandler, tm);
	assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
	type.nullSafeSet(ps, content, 1);
	Synchronization synch = transaction.getSynchronization();
	assertNotNull(synch);
	synch.beforeCompletion();
	synch.afterCompletion(Status.STATUS_COMMITTED);
	verify(lobCreator).setBlobAsBytes(ps, 1, content);
}
 
Example 3
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testClobStringTypeWithJtaSynchronizationAndRollback() throws Exception {
	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.getTransaction()).willReturn(transaction);
	given(lobHandler.getClobAsString(rs, "column")).willReturn("content");

	ClobStringType type = new ClobStringType(lobHandler, tm);
	assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
	type.nullSafeSet(ps, "content", 1);
	Synchronization synch = transaction.getSynchronization();
	assertNotNull(synch);
	synch.afterCompletion(Status.STATUS_ROLLEDBACK);

	verify(lobCreator).setClobAsString(ps, 1, "content");
}
 
Example 4
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testClobStringTypeWithJtaSynchronization() throws Exception {
	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.getTransaction()).willReturn(transaction);

	given(lobHandler.getClobAsString(rs, "column")).willReturn("content");

	ClobStringType type = new ClobStringType(lobHandler, tm);
	assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
	type.nullSafeSet(ps, "content", 1);
	Synchronization synch = transaction.getSynchronization();
	assertNotNull(synch);
	synch.beforeCompletion();
	synch.afterCompletion(Status.STATUS_COMMITTED);
	verify(lobCreator).setClobAsString(ps, 1, "content");
}
 
Example 5
Source File: MithraRemoteTransactionProxy.java    From reladomo with Apache License 2.0 5 votes vote down vote up
private void afterCompletion()
{
    if (this.synchronizations != null)
    {
        for (int i = 0; i < synchronizations.size(); i++)
        {
            Synchronization s = (Synchronization) synchronizations.get(i);
            s.afterCompletion(this.proxyStatus);
        }
    }

}
 
Example 6
Source File: TransactionSynchronizer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Invoke an afterCompletion
 * 
 * @param synch the synchronization
 * @param status the status of the transaction
 */
protected void invokeAfter(Synchronization synch, int status)
{
   try
   {
      synch.afterCompletion(status);
   }
   catch (Throwable t)
   {
      log.transactionErrorInAfterCompletion(tx, synch, t);
   }
}
 
Example 7
Source File: MultiThreadedTx.java    From reladomo with Apache License 2.0 5 votes vote down vote up
private void afterCompletion(int status)
{
    for(int i=0;i<this.synchronizations.size();i++)
    {
        Synchronization synch = synchronizations.get(i);
        try
        {
            synch.afterCompletion(status);
        }
        catch(Throwable t)
        {
            this.logger.error("error calling afterCompletion on synch "+synch, t);
        }
    }
}
 
Example 8
Source File: DummyTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void rollback() throws IllegalStateException, SystemException {

		status = Status.STATUS_ROLLING_BACK;

// Synch.beforeCompletion() should *not* be called for rollbacks
//		for ( int i=0; i<synchronizations.size(); i++ ) {
//			Synchronization s = (Synchronization) synchronizations.get(i);
//			s.beforeCompletion();
//		}
		
		status = Status.STATUS_ROLLEDBACK;
		
		try {
			connection.rollback();
			connection.close();
		}
		catch (SQLException sqle) {
			status = Status.STATUS_UNKNOWN;
			throw new SystemException();
		}
		
		for ( int i=0; i<synchronizations.size(); i++ ) {
			Synchronization s = (Synchronization) synchronizations.get(i);
			s.afterCompletion(status);
		}
		
		//status = Status.STATUS_NO_TRANSACTION;
		transactionManager.endCurrent(this);
	}
 
Example 9
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlobSerializableTypeWithJtaSynchronization() throws Exception {
	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.getTransaction()).willReturn(transaction);

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	oos.writeObject("content");
	oos.close();

	given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn(
			new ByteArrayInputStream(baos.toByteArray()));

	BlobSerializableType type = new BlobSerializableType(lobHandler, tm);
	assertEquals(1, type.sqlTypes().length);
	assertEquals(Types.BLOB, type.sqlTypes()[0]);
	assertEquals(Serializable.class, type.returnedClass());
	assertTrue(type.isMutable());

	assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
	type.nullSafeSet(ps, "content", 1);
	Synchronization synch = transaction.getSynchronization();
	assertNotNull(synch);
	synch.beforeCompletion();
	synch.afterCompletion(Status.STATUS_COMMITTED);
	verify(lobCreator).setBlobAsBytes(ps, 1, baos.toByteArray());
}
 
Example 10
Source File: JDBCTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void notifyLocalSynchsAfterTransactionCompletion(int status) {
	begun = false;
	if (synchronizations!=null) {
		for ( int i=0; i<synchronizations.size(); i++ ) {
			Synchronization sync = (Synchronization) synchronizations.get(i);
			try {
				sync.afterCompletion(status);
			}
			catch (Throwable t) {
				log.error("exception calling user Synchronization", t);
			}
		}
	}
}
 
Example 11
Source File: HibernateJtaTransactionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testJtaSessionSynchronizationWithRollback() throws Exception {

	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	given(tm.getTransaction()).willReturn(transaction);
	final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
	final Session session = mock(Session.class);
	given(sf.openSession()).willReturn(session);
	given(sf.getTransactionManager()).willReturn(tm);
	given(session.isOpen()).willReturn(true);

	assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
	HibernateTemplate ht = new HibernateTemplate(sf);
	ht.setExposeNativeSession(true);
	for (int i = 0; i < 5; i++) {
		ht.executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(org.hibernate.Session sess) {
				assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
				assertEquals(session, sess);
				return null;
			}
		});
	}

	Synchronization synchronization = transaction.getSynchronization();
	assertTrue("JTA synchronization registered", synchronization != null);
	synchronization.afterCompletion(Status.STATUS_ROLLEDBACK);

	assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
	assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

	verify(session).close();
}
 
Example 12
Source File: SynchronizationRegistryImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void notifySynchronizationsAfterTransactionCompletion(int status) {
	if ( synchronizations != null ) {
		for ( Synchronization synchronization : synchronizations ) {
			try {
				synchronization.afterCompletion( status );
			}
			catch ( Throwable t ) {
				LOG.synchronizationFailed( synchronization, t );
			}
		}
	}
}
 
Example 13
Source File: TransactionImpl.java    From clearpool with GNU General Public License v3.0 5 votes vote down vote up
private void afterCompletion(int st) {
  if (this.synList == null) {
    return;
  }
  for (Synchronization syn : this.synList) {
    syn.afterCompletion(st);
  }
}
 
Example 14
Source File: SynchronizationRegistryStandardImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void notifySynchronizationsAfterTransactionCompletion(int status) {
	log.tracef(
			"SynchronizationRegistryStandardImpl.notifySynchronizationsAfterTransactionCompletion(%s)",
			status
	);

	if ( synchronizations != null ) {
		try {
			for ( Synchronization synchronization : synchronizations ) {
				try {
					synchronization.afterCompletion( status );
				}
				catch (Throwable t) {
					log.synchronizationFailed( synchronization, t );
					throw new LocalSynchronizationException(
							"Exception calling user Synchronization (afterCompletion): " + synchronization.getClass().getName(),
							t
					);
				}
			}
		}
		finally {
			clearSynchronizations();
		}
	}
}
 
Example 15
Source File: HibernateJtaTransactionTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testJtaSessionSynchronizationWithRemoteTransaction() throws Exception {

	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
	final Session session = mock(Session.class);
	given(tm.getTransaction()).willReturn(transaction);
	given(sf.openSession()).willReturn(session);
	given(sf.getTransactionManager()).willReturn(tm);
	given(session.isOpen()).willReturn(true);
	given(session.getFlushMode()).willReturn(FlushMode.AUTO);

	for (int j = 0; j < 2; j++) {

		if (j == 0) {
			assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
		}
		else {
			assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
		}

		HibernateTemplate ht = new HibernateTemplate(sf);
		ht.setExposeNativeSession(true);
		for (int i = 0; i < 5; i++) {
			ht.executeFind(new HibernateCallback() {

				@Override
				public Object doInHibernate(org.hibernate.Session sess) {
					assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
					assertEquals(session, sess);
					return null;
				}
			});
		}

		final Synchronization synchronization = transaction.getSynchronization();
		assertTrue("JTA synchronization registered", synchronization != null);

		// Call synchronization in a new thread, to simulate a
		// synchronization
		// triggered by a new remote call from a remote transaction
		// coordinator.
		Thread synch = new Thread() {

			@Override
			public void run() {
				synchronization.beforeCompletion();
				synchronization.afterCompletion(Status.STATUS_COMMITTED);
			}
		};
		synch.start();
		synch.join();

		assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
		SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
		assertTrue("Thread session holder empty", sessionHolder.isEmpty());
		assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
	}

	verify(session, times(2)).flush();
	verify(session, times(2)).close();
	TransactionSynchronizationManager.unbindResource(sf);
}
 
Example 16
Source File: HibernateJtaTransactionTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
private void doTestJtaSessionSynchronizationWithPreBound(boolean flushNever) throws Exception {

	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	given(tm.getTransaction()).willReturn(transaction);
	final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
	final Session session = mock(Session.class);
	given(sf.getTransactionManager()).willReturn(tm);
	given(session.isOpen()).willReturn(true);
	if (flushNever) {
		given(session.getFlushMode()).willReturn(FlushMode.MANUAL, FlushMode.AUTO, FlushMode.MANUAL);
	}
	else {
		given(session.getFlushMode()).willReturn(FlushMode.AUTO);
	}

	assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
	TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
	try {
		assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
		HibernateTemplate ht = new HibernateTemplate(sf);
		ht.setExposeNativeSession(true);
		for (int i = 0; i < 5; i++) {
			ht.executeFind(new HibernateCallback() {

				@Override
				public Object doInHibernate(org.hibernate.Session sess) {
					assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
					assertEquals(session, sess);
					return null;
				}
			});
		}

		Synchronization synchronization = transaction.getSynchronization();
		assertTrue("JTA synchronization registered", synchronization != null);
		synchronization.beforeCompletion();
		synchronization.afterCompletion(Status.STATUS_COMMITTED);
		assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
	}
	finally {
		TransactionSynchronizationManager.unbindResource(sf);
	}
	assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));

	InOrder ordered = inOrder(session);
	if(flushNever) {
		ordered.verify(session).setFlushMode(FlushMode.AUTO);
		ordered.verify(session).setFlushMode(FlushMode.MANUAL);
	} else {
		ordered.verify(session).flush();
	}
	ordered.verify(session).disconnect();
}
 
Example 17
Source File: HibernateJtaTransactionTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testJtaSessionSynchronizationWithNonSessionFactoryImplementor() throws Exception {

	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	given(tm.getTransaction()).willReturn(transaction);
	final SessionFactory sf = mock(SessionFactory.class);
	final Session session = mock(Session.class);
	final SessionFactoryImplementor sfi = mock(SessionFactoryImplementor.class);
	given(sf.openSession()).willReturn(session);
	given(session.getSessionFactory()).willReturn(sfi);
	given(sfi.getTransactionManager()).willReturn(tm);
	given(session.isOpen()).willReturn(true);
	given(session.getFlushMode()).willReturn(FlushMode.AUTO);

	assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
	HibernateTemplate ht = new HibernateTemplate(sf);
	ht.setExposeNativeSession(true);
	for (int i = 0; i < 5; i++) {
		ht.executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(org.hibernate.Session sess) {
				assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
				assertEquals(session, sess);
				return null;
			}
		});
	}

	Synchronization synchronization = transaction.getSynchronization();
	assertTrue("JTA Synchronization registered", synchronization != null);
	synchronization.beforeCompletion();
	synchronization.afterCompletion(Status.STATUS_COMMITTED);

	assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
	assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

	verify(session).flush();
	verify(session).close();
}
 
Example 18
Source File: WebSphereExtendedJTATransactionLookup.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void registerSynchronization(final Synchronization synchronization)
		throws RollbackException, IllegalStateException,
		SystemException {

	final InvocationHandler ih = new InvocationHandler() {

		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
			if ( "afterCompletion".equals( method.getName() ) ) {
				int status = args[2].equals(Boolean.TRUE) ?
						Status.STATUS_COMMITTED :
						Status.STATUS_UNKNOWN;
				synchronization.afterCompletion(status);
			}
			else if ( "beforeCompletion".equals( method.getName() ) ) {
				synchronization.beforeCompletion();
			}
			else if ( "toString".equals( method.getName() ) ) {
				return synchronization.toString();
			}
			return null;
		}

	};

	final Object synchronizationCallback = Proxy.newProxyInstance(
			getClass().getClassLoader(),
			new Class[] { synchronizationCallbackClass },
			ih
		);

	try {
		registerSynchronizationMethod.invoke(
				extendedJTATransaction,
				new Object[] { synchronizationCallback }
			);
	}
	catch (Exception e) {
		throw new HibernateException(e);
	}

}
 
Example 19
Source File: HibernateJtaTransactionTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testJtaSessionSynchronizationWithFlushFailure() throws Exception {

	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	given(tm.getTransaction()).willReturn(transaction);
	final HibernateException flushEx = new HibernateException("flush failure");
	final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
	final Session session = mock(Session.class);
	given(sf.openSession()).willReturn(session);
	given(sf.getTransactionManager()).willReturn(tm);
	given(session.isOpen()).willReturn(true);
	given(session.getFlushMode()).willReturn(FlushMode.AUTO);
	willThrow(flushEx).given(session).flush();

	assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
	HibernateTemplate ht = new HibernateTemplate(sf);
	ht.setExposeNativeSession(true);
	for (int i = 0; i < 5; i++) {
		ht.executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(org.hibernate.Session sess) {
				assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
				assertEquals(session, sess);
				return null;
			}
		});
	}

	Synchronization synchronization = transaction.getSynchronization();
	assertTrue("JTA synchronization registered", synchronization != null);
	try {
		synchronization.beforeCompletion();
		fail("Should have thrown HibernateSystemException");
	}
	catch (HibernateSystemException ex) {
		assertSame(flushEx, ex.getCause());
	}
	synchronization.afterCompletion(Status.STATUS_ROLLEDBACK);

	assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
	assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

	verify(tm).setRollbackOnly();
	verify(session).close();
}
 
Example 20
Source File: XAStoreTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
private void fireAfterCompletion(int status) {
  for (Synchronization synchronization : synchronizations) {
    synchronization.afterCompletion(status);
  }
}