KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > transaction > CacheSynchronization


1 //$Id: CacheSynchronization.java,v 1.7 2005/04/23 23:12:05 oneovthafew Exp $
2
package org.hibernate.transaction;
3
4 import javax.transaction.Status JavaDoc;
5 import javax.transaction.Synchronization JavaDoc;
6 import javax.transaction.SystemException JavaDoc;
7 import javax.transaction.Transaction JavaDoc;
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 /**
16  * @author Gavin King
17  */

18 public final class CacheSynchronization implements Synchronization JavaDoc {
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 JavaDoc transaction;
25     private final org.hibernate.Transaction hibernateTransaction;
26
27     public CacheSynchronization(
28             TransactionFactory.Context ctx,
29             JDBCContext jdbcContext,
30             Transaction JavaDoc 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                     //actually, this last test is probably unnecessary, since
48
//beforeCompletion() doesn't get called during rollback
49
}
50         catch (SystemException JavaDoc 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 JavaDoc toString() {
82         return CacheSynchronization.class.getName();
83     }
84
85 }
86
Popular Tags