當前位置: 首頁>>代碼示例>>Java>>正文


Java Synchronization類代碼示例

本文整理匯總了Java中javax.transaction.Synchronization的典型用法代碼示例。如果您正苦於以下問題:Java Synchronization類的具體用法?Java Synchronization怎麽用?Java Synchronization使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Synchronization類屬於javax.transaction包,在下文中一共展示了Synchronization類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getConnection

import javax.transaction.Synchronization; //導入依賴的package包/類
@Override
public Connection getConnection() {
    // If TransactionSynchronizationRegistry exists,
    // register Synchronization to release connection automatically
    // at the first invocation of this method in the current thread.
    // TODO: I wonder if I can register for each thread ...
    if(syncRegistry != null && registered.get() == null){
        syncRegistry.registerInterposedSynchronization(new Synchronization() {
            //@Override
            public void beforeCompletion() {
            }

            //@Override
            public void afterCompletion(int status) {
                releaseConnection();
                registered.remove();
            }
        });
        registered.set(true);
    }

    return super.getConnection();
}
 
開發者ID:mirage-sql,項目名稱:mirage-integration,代碼行數:24,代碼來源:SeasarConnectionProvider.java

示例2: testClobStringTypeWithJtaSynchronization

import javax.transaction.Synchronization; //導入依賴的package包/類
@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");
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:19,代碼來源:LobTypeTests.java

示例3: testClobStringTypeWithJtaSynchronizationAndRollback

import javax.transaction.Synchronization; //導入依賴的package包/類
@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");
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:18,代碼來源:LobTypeTests.java

示例4: testBlobStringTypeWithJtaSynchronization

import javax.transaction.Synchronization; //導入依賴的package包/類
@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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:22,代碼來源:LobTypeTests.java

示例5: testBlobStringTypeWithJtaSynchronizationAndRollback

import javax.transaction.Synchronization; //導入依賴的package包/類
@Test
public void testBlobStringTypeWithJtaSynchronizationAndRollback() 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.afterCompletion(Status.STATUS_ROLLEDBACK);
	verify(lobCreator).setBlobAsBytes(ps, 1, contentBytes);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:20,代碼來源:LobTypeTests.java

示例6: testBlobByteArrayTypeWithJtaSynchronization

import javax.transaction.Synchronization; //導入依賴的package包/類
@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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:20,代碼來源:LobTypeTests.java

示例7: testBlobByteArrayTypeWithJtaSynchronizationAndRollback

import javax.transaction.Synchronization; //導入依賴的package包/類
@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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:19,代碼來源:LobTypeTests.java

示例8: testRollbackNoExisting

import javax.transaction.Synchronization; //導入依賴的package包/類
@Test public void testRollbackNoExisting() throws Exception {
	setupTransaction(Connection.TRANSACTION_SERIALIZABLE);
	execute("create local temporary table x (e1 string, e2 integer)", new List[] {Arrays.asList(0)}); //$NON-NLS-1$
	execute("insert into x (e2, e1) select e2, e1 from pm1.g1", new List[] {Arrays.asList(6)}); //$NON-NLS-1$
	execute("update x set e1 = e2 where e2 > 1", new List[] {Arrays.asList(2)}); //$NON-NLS-1$
	
	Mockito.verify(txn).registerSynchronization((Synchronization) Mockito.anyObject());
	synch.afterCompletion(Status.STATUS_ROLLEDBACK);
	
	try {
		execute("select * from x", new List[] {});
		fail();
	} catch (Exception e) {
		
	}
	execute("create local temporary table x (e1 string, e2 integer)", new List[] {Arrays.asList(0)}); //$NON-NLS-1$
}
 
開發者ID:kenweezy,項目名稱:teiid,代碼行數:18,代碼來源:TestTempTables.java

示例9: doWithSyncs

import javax.transaction.Synchronization; //導入依賴的package包/類
/**
 * Experience shows that in some cases the synchronization objects are added
 * while we are doing the commits as a result of EntityListeners for
 * example. Therefore prevent concurrent modification exceptions.
 * 
 * @param c The consumer to execute on all synchronization objects
 */
private void doWithSyncs(Consumer<Synchronization> c) {
    if (toSync.size() == 0) return;
    List<Synchronization> toDo = toSync;
    toSync = new ArrayList<>();
    try {
        // Perform the consumer on the existing list.
        toDo.stream().forEach((s) -> c.accept(s));
        // And on the new list recursively.
        doWithSyncs(c);
    } finally {
        // Gather the lot to the new list.
        toDo.addAll(toSync);
        toSync = toDo;
    }
}
 
開發者ID:arievanwi,項目名稱:osgi.ee,代碼行數:23,代碼來源:TransactionImpl.java

示例10: invoke

import javax.transaction.Synchronization; //導入依賴的package包/類
@AroundInvoke
public Object invoke(final InvocationContext ctx) throws Exception {
    boolean transactional = false;
    try {
        System.out.println("Intercepting "+ctx.getMethod().toGenericString());
        transactional = txRegistry != null && txRegistry.getTransactionStatus() != Status.STATUS_NO_TRANSACTION;
        if (transactional) {
            txRegistry.registerInterposedSynchronization(new Synchronization() {
                @Override
                public void beforeCompletion() {

                }

                @Override
                public void afterCompletion(int i) {
                    registerInvocation(ctx.getMethod());
                }
            });
        }
        return ctx.proceed();
    } finally {
        if (!transactional) {
            registerInvocation(ctx.getMethod());
        }
    }
}
 
開發者ID:ftomassetti,項目名稱:JavaIncrementalParser,代碼行數:27,代碼來源:ReceptionSynchronizer.java

示例11: registerSynchronization

import javax.transaction.Synchronization; //導入依賴的package包/類
@Override
public void registerSynchronization(Synchronization synchronization)
    throws RollbackException, SystemException {
  if (this.status != Status.STATUS_ACTIVE) {
    throw new IllegalStateException("the transaction is not active");
  }
  if (this.rollbackOnly) {
    throw new RollbackException("the transaction is signed to roll back only");
  }
  TransactionAdapter txAdapt =
      (TransactionAdapter) TransactionManagerImpl.getManager().getTransaction();
  if (txAdapt.getTx() != this) {
    throw new IllegalStateException("the transaction is not held");
  }
  if (this.synList == null) {
    this.synList = new ArrayList<Synchronization>();
  }
  this.synList.add(synchronization);
}
 
開發者ID:xionghuiCoder,項目名稱:clearpool,代碼行數:20,代碼來源:TransactionImpl.java

示例12: testCommit

import javax.transaction.Synchronization; //導入依賴的package包/類
@Test
public void testCommit() {
	Transaction tx = new Transaction();
	boolean[] tmp = new boolean[2];
	tx.registerSynchronization(
			new Synchronization() {
				@Override
				public void beforeCompletion() {
					tmp[0] = true;
				}

				@Override
				public void afterCompletion(int status) {
					if ( status == Status.STATUS_COMMITTED ) {
						tmp[1] = true;
					}
				}
			}
	);
	tx.commit();
	for ( boolean t : tmp ) {
		assertTrue( t );
	}
}
 
開發者ID:Hotware,項目名稱:Hibernate-Search-GenericJPA,代碼行數:25,代碼來源:TransactionTest.java

示例13: testRollback

import javax.transaction.Synchronization; //導入依賴的package包/類
@Test
public void testRollback() {
	Transaction tx = new Transaction();
	boolean[] tmp = new boolean[2];
	tx.registerSynchronization(
			new Synchronization() {
				@Override
				public void beforeCompletion() {
					tmp[0] = true;
				}

				@Override
				public void afterCompletion(int status) {
					if ( status == Status.STATUS_ROLLEDBACK ) {
						tmp[1] = true;
					}
				}
			}
	);
	tx.rollback();
	for ( boolean t : tmp ) {
		assertTrue( t );
	}
}
 
開發者ID:Hotware,項目名稱:Hibernate-Search-GenericJPA,代碼行數:25,代碼來源:TransactionTest.java

示例14: registerInterposedSynchronization

import javax.transaction.Synchronization; //導入依賴的package包/類
/**
 * {@inheritDoc}
 */
public void registerInterposedSynchronization(Synchronization sync)
{
   TransactionImpl tx = registry.getTransaction();

   if (tx == null)
      throw new IllegalStateException();

   try
   {
      tx.registerSynchronization(sync);
   }
   catch (Throwable t)
   {
      // Nothing to do
   }
}
 
開發者ID:ironjacamar,項目名稱:ironjacamar,代碼行數:20,代碼來源:TransactionSynchronizationRegistryImpl.java

示例15: doAfterCompletion

import javax.transaction.Synchronization; //導入依賴的package包/類
synchronized void doAfterCompletion()
{
    for ( Synchronization s : syncHooks )
    {
        try
        {
            s.afterCompletion( status );
        }
        catch ( Throwable t )
        {
            log.log( Level.WARNING, "Caught exception from tx syncronization[" + s
                + "] afterCompletion()", t );
        }
    }
    syncHooks = null; // help gc
}
 
開發者ID:neo4j-contrib,項目名稱:neo4j-mobile-android,代碼行數:17,代碼來源:TransactionImpl.java


注:本文中的javax.transaction.Synchronization類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。