1 24 25 package com.rift.coad.lib.transaction; 27 28 import javax.transaction.UserTransaction ; 30 import javax.transaction.TransactionManager ; 31 32 import org.apache.log4j.Logger; 34 35 import org.objectweb.jotm.Jotm; 37 38 import com.rift.coad.lib.naming.ContextManager; 40 41 48 public class TransactionDirector { 49 50 private final static String TRANSACTION_CONTEXT_BASE = 52 "java:comp/"; 53 private final static String USER_TRANSACTION_CONTEXT_BINDING = 54 "UserTransaction"; 55 private final static String TRANSACTION_MANAGER_CONTEXT_BINDING = 56 "TransactionManager"; 57 58 private Logger log = Logger.getLogger(TransactionDirector.class.getName()); 60 61 private static TransactionDirector singleton = null; 63 private Jotm jotm = null; 64 private UserTransaction userTransaction = null; 65 private TransactionManager transactionManager = null; 66 67 70 public TransactionDirector() throws TransactionException { 71 try { 72 jotm = new Jotm(true,true); 73 ContextManager contextManager = 74 new ContextManager(TRANSACTION_CONTEXT_BASE); 75 userTransaction = jotm.getUserTransaction(); 76 contextManager.bind(USER_TRANSACTION_CONTEXT_BINDING, 77 userTransaction); 78 transactionManager = jotm.getTransactionManager(); 79 contextManager.bind(TRANSACTION_MANAGER_CONTEXT_BINDING, 80 transactionManager); 81 } catch (Exception ex) { 82 throw new TransactionException( 83 "Failed to instanciate the transaction directory because : " 84 + ex.getMessage(),ex); 85 } 86 } 87 88 89 95 public static synchronized TransactionDirector init() 96 throws TransactionException { 97 if (singleton == null) { 98 singleton = new TransactionDirector(); 99 } 100 return singleton; 101 } 102 103 104 110 public static synchronized TransactionDirector getInstance() 111 throws TransactionException { 112 if (singleton == null) { 113 throw new TransactionException( 114 "The transaction director is not instanciated."); 115 } 116 return singleton; 117 } 118 119 120 125 public UserTransaction getUserTransaction() { 126 return userTransaction; 127 } 128 129 130 135 public TransactionManager getTransactionManager() { 136 return transactionManager; 137 } 138 139 140 143 public void stop() { 144 try { 145 jotm.stop(); 146 } catch (Exception ex) { 147 log.error("Failed to shut down the transaction manager : " + 148 ex.getMessage(),ex); 149 } 150 } 151 } 152 | Popular Tags |