本文整理汇总了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();
}
示例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");
}
示例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");
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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$
}
示例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;
}
}
示例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());
}
}
}
示例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);
}
示例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 );
}
}
示例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 );
}
}
示例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
}
}
示例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
}