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

The following examples show how to use javax.transaction.Synchronization#beforeCompletion() . 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: SynchronizationRegistryStandardImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void notifySynchronizationsBeforeTransactionCompletion() {
	log.trace( "SynchronizationRegistryStandardImpl.notifySynchronizationsBeforeTransactionCompletion" );

	if ( synchronizations != null ) {
		for ( Synchronization synchronization : synchronizations ) {
			try {
				synchronization.beforeCompletion();
			}
			catch (Throwable t) {
				log.synchronizationFailed( synchronization, t );
				throw new LocalSynchronizationException(
						"Exception calling user Synchronization (beforeCompletion): " + synchronization.getClass().getName(),
						t
				);
			}
		}
	}
}
 
Example 2
Source File: MultiThreadedTx.java    From reladomo with Apache License 2.0 6 votes vote down vote up
private void beforeCompletion()
{
    try
    {
        for(int i=0;i<this.synchronizations.size();i++)
        {
            Synchronization s = this.synchronizations.get(i);
            s.beforeCompletion();
        }
    }
    catch(Throwable t)
    {
        logger.error("The synchronization before completion failed. marked for rollback ", t);
        this.status.set(MARKED_ROLLBACK);
    }
}
 
Example 3
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 4
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlobStringTypeWithJtaSynchronization() throws Exception {
	TransactionManager tm = mock(TransactionManager.class);
	MockJtaTransaction transaction = new MockJtaTransaction();
	given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.getTransaction()).willReturn(transaction);

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

	BlobStringType type = new BlobStringType(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, contentBytes);
}
 
Example 5
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 6
Source File: HibernateJtaTransactionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testJtaSessionSynchronization() 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);
	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 7
Source File: SynchronizationRegistryImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void notifySynchronizationsBeforeTransactionCompletion() {
	if ( synchronizations != null ) {
		for ( Synchronization synchronization : synchronizations ) {
			try {
				synchronization.beforeCompletion();
			}
			catch ( Throwable t ) {
				LOG.synchronizationFailed( synchronization, t );
			}
		}
	}
}
 
Example 8
Source File: WebSphereExtendedJtaPlatform.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void registerSynchronization(final Synchronization synchronization)
		throws RollbackException, IllegalStateException,
		SystemException {

	final InvocationHandler ih = new InvocationHandler() {

		@Override
		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, synchronizationCallback );
	}
	catch (Exception e) {
		throw new HibernateException(e);
	}

}
 
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: TransactionSynchronizer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Invoke a beforeCompletion
 * 
 * @param synch the synchronization
 */
protected void invokeBefore(Synchronization synch)
{
   try
   {
      synch.beforeCompletion();
   }
   catch (Throwable t)
   {
      log.transactionErrorInBeforeCompletion(tx, synch, t);
   }
}
 
Example 11
Source File: JDBCTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void notifyLocalSynchsBeforeTransactionCompletion() {
	if (synchronizations!=null) {
		for ( int i=0; i<synchronizations.size(); i++ ) {
			Synchronization sync = (Synchronization) synchronizations.get(i);
			try {
				sync.beforeCompletion();
			}
			catch (Throwable t) {
				log.error("exception calling user Synchronization", t);
			}
		}
	}
}
 
Example 12
Source File: SynchronizationList.java    From ByteJTA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public synchronized void beforeCompletion() {
	if (this.beforeCompletionInvoked == false) {
		int length = this.synchronizations.size();
		for (int i = 0; i < length; i++) {
			Synchronization synchronization = this.synchronizations.get(i);
			try {
				synchronization.beforeCompletion();
			} catch (RuntimeException error) {
				logger.error(error.getMessage(), error);
			}
		} // end-for

		this.beforeCompletionInvoked = true;
	} // end-if (this.beforeCompletionInvoked == false)
}
 
Example 13
Source File: TransactionImpl.java    From clearpool with GNU General Public License v3.0 5 votes vote down vote up
private void beforeCompletion() {
  if (this.synList == null) {
    return;
  }
  for (Synchronization syn : this.synList) {
    syn.beforeCompletion();
  }
}
 
Example 14
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 15
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 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 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 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: XAStoreTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
private void fireBeforeCompletion() {
  for (Synchronization synchronization : synchronizations) {
    synchronization.beforeCompletion();
  }
}
 
Example 20
Source File: PseudoTransactionService.java    From tomee with Apache License 2.0 4 votes vote down vote up
private void doBeforeCompletion() {
    for (final Synchronization sync : new ArrayList<>(registeredSynchronizations)) {
        sync.beforeCompletion();
    }
}