1 package org.hibernate.transaction; 3 4 import javax.transaction.Status ; 5 import javax.transaction.Synchronization ; 6 import javax.transaction.SystemException ; 7 import javax.transaction.Transaction ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.hibernate.TransactionException; 12 import org.hibernate.jdbc.JDBCContext; 13 import org.hibernate.util.JTAHelper; 14 15 18 public final class CacheSynchronization implements Synchronization { 19 20 private static final Log log = LogFactory.getLog(CacheSynchronization.class); 21 22 private final TransactionFactory.Context ctx; 23 private JDBCContext jdbcContext; 24 private final Transaction transaction; 25 private final org.hibernate.Transaction hibernateTransaction; 26 27 public CacheSynchronization( 28 TransactionFactory.Context ctx, 29 JDBCContext jdbcContext, 30 Transaction transaction, 31 org.hibernate.Transaction tx 32 ) { 33 this.ctx = ctx; 34 this.jdbcContext = jdbcContext; 35 this.transaction = transaction; 36 this.hibernateTransaction = tx; 37 } 38 39 public void beforeCompletion() { 40 log.trace("transaction before completion callback"); 41 42 boolean flush; 43 try { 44 flush = !ctx.isFlushModeNever() && 45 ctx.isFlushBeforeCompletionEnabled() && 46 !JTAHelper.isRollback( transaction.getStatus() ); 47 } 50 catch (SystemException se) { 51 log.error("could not determine transaction status", se); 52 throw new TransactionException("could not determine transaction status in beforeCompletion()", se); 53 } 54 55 try { 56 if (flush) { 57 log.trace("automatically flushing session"); 58 ctx.managedFlush(); 59 } 60 } 61 finally { 62 jdbcContext.beforeTransactionCompletion(hibernateTransaction); 63 } 64 } 65 66 public void afterCompletion(int status) { 67 if ( log.isTraceEnabled() ) { 68 log.trace("transaction after completion callback, status: " + status); 69 } 70 try { 71 jdbcContext.afterTransactionCompletion(status==Status.STATUS_COMMITTED, hibernateTransaction); 72 } 73 finally { 74 if ( ctx.shouldAutoClose() && ctx.isOpen() ) { 75 log.trace("automatically closing session"); 76 ctx.managedClose(); 77 } 78 } 79 } 80 81 public String toString() { 82 return CacheSynchronization.class.getName(); 83 } 84 85 } 86 | Popular Tags |