1 16 package com.ibatis.dao.engine.impl; 17 18 import com.ibatis.dao.client.Dao; 19 import com.ibatis.dao.client.DaoException; 20 import com.ibatis.dao.client.DaoTransaction; 21 import com.ibatis.dao.engine.transaction.DaoTransactionManager; 22 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 public class DaoContext { 28 29 private String id; 30 private StandardDaoManager daoManager; 31 private DaoTransactionManager transactionManager; 32 private ThreadLocal transaction = new ThreadLocal (); 33 private ThreadLocal state = new ThreadLocal (); 34 35 private Map typeDaoImplMap = new HashMap (); 36 37 public DaoContext() { 38 } 39 40 public String getId() { 41 return id; 42 } 43 44 public void setId(String id) { 45 this.id = id; 46 } 47 48 public StandardDaoManager getDaoManager() { 49 return daoManager; 50 } 51 52 public void setDaoManager(StandardDaoManager daoManager) { 53 this.daoManager = daoManager; 54 } 55 56 public DaoTransactionManager getTransactionManager() { 57 return transactionManager; 58 } 59 60 public void setTransactionManager(DaoTransactionManager transactionManager) { 61 this.transactionManager = transactionManager; 62 } 63 64 public void addDao(DaoImpl daoImpl) { 65 if (typeDaoImplMap.containsKey(daoImpl.getDaoInterface())) { 66 throw new DaoException("More than one implementation for '" + daoImpl.getDaoInterface() + "' was configured. " + 67 "Only one implementation per context is allowed."); 68 } 69 typeDaoImplMap.put(daoImpl.getDaoInterface(), daoImpl); 70 } 71 72 public Dao getDao(Class iface) { 73 DaoImpl impl = (DaoImpl) typeDaoImplMap.get(iface); 74 if (impl == null) { 75 throw new DaoException("There is no DAO implementation found for " + iface + " in this context."); 76 } 77 return impl.getProxy(); 78 } 79 80 public Iterator getDaoImpls() { 81 return typeDaoImplMap.values().iterator(); 82 } 83 84 public DaoTransaction getTransaction() { 85 startTransaction(); 86 return (DaoTransaction) transaction.get(); 87 } 88 89 public void startTransaction() { 90 if (state.get() != DaoTransactionState.ACTIVE) { 91 DaoTransaction trans = transactionManager.startTransaction(); 92 transaction.set(trans); 93 state.set(DaoTransactionState.ACTIVE); 94 daoManager.addContextInTransaction(this); 95 } 96 } 97 98 public void commitTransaction() { 99 DaoTransaction trans = (DaoTransaction) transaction.get(); 100 if (state.get() == DaoTransactionState.ACTIVE) { 101 transactionManager.commitTransaction(trans); 102 state.set(DaoTransactionState.COMMITTED); 103 } else { 104 state.set(DaoTransactionState.INACTIVE); 105 } 106 } 107 108 public void endTransaction() { 109 DaoTransaction trans = (DaoTransaction) transaction.get(); 110 if (state.get() == DaoTransactionState.ACTIVE) { 111 try { 112 transactionManager.rollbackTransaction(trans); 113 } finally { 114 state.set(DaoTransactionState.ROLLEDBACK); 115 transaction.set(null); 116 } 117 } else { 118 state.set(DaoTransactionState.INACTIVE); 119 transaction.set(null); 120 } 121 } 122 123 } 124 | Popular Tags |