- final
- public class TransManager {
- private final static Log log = LogUtil.getLog(TransManager.class);
- private final static ThreadLocal<Map<String,Object>> cache = new ThreadLocal<Map<String,Object>>(){
- @Override
- protected Map<String,Object> initialValue() {
- return new HashMap<String,Object>();
- }
- };
- //--------------------------------------
- private static List<TransListener> listenerLst = new CopyOnWriteArrayList<TransListener>();
- public static void addListener(TransListener listener){
- if(listener == null){
- throw new java.lang.NullPointerException();
- }
- listenerLst.add(listener);
- }
- public static void remove(TransListener listener){
- if(listener == null){
- throw new java.lang.NullPointerException();
- }
- listenerLst.remove(listener);
- }
- //--------------------------------------
- public static <T> T wrap(Class<T> clz) {
- return wrap(clz, false , true);
- }
- public static <T> T wrap(Class<T> clz , boolean isUseCache) {
- return wrap(clz, false , isUseCache);
- }
- public static <T> T wrap(Class<T> clz , boolean isReadOnly , boolean isUseCache) {
- return wrap(clz, DbSessionFactory.MAIN , isReadOnly , isUseCache);
- }
- @SuppressWarnings("unchecked")
- public static <T> T wrap(Class<T> clz , int dbtype , boolean isReadOnly , boolean isUseCache) {
- if(!isUseCache){
- return makeObj(clz,dbtype,isReadOnly);
- }else{
- String key = clz.getCanonicalName()+"_"+dbtype+"_"+isReadOnly;
- T obj = (T)cache.get().get(key);
- if(obj == null){
- obj = makeObj(clz,dbtype,isReadOnly);
- cache.get().put(key,obj);
- }
- return obj;
- }
- }
- @SuppressWarnings("unchecked")
- private static <T> T makeObj(Class<T> clz,final int dbtype , final boolean isReadOnly){
- return (T)Enhancer.create(clz, new MethodInterceptor(){
- public Object intercept(Object obj, Method method, Object[] arg,MethodProxy proxy) throws Throwable {
- return execute(dbtype, isReadOnly, obj, null , proxy , arg);
- }
- });
- }
- //---------------------------------
- public static <T> T doTransaction(Transaction<T> t) throws Exception {
- return doTransaction(t , DbSessionFactory.MAIN , false);
- }
- private static Method exeMethod ;
- static{
- try {
- exeMethod = Transaction.class.getMethod("execute");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @SuppressWarnings("unchecked")
- public static <T> T doTransaction(Transaction<T> t , int dbtype , boolean isReadOnly) throws Exception {
- return (T)execute(dbtype, isReadOnly, t, exeMethod , null , null);
- }
- //----------------------------------
- private static Object execute(int dbtype , boolean isReadOnly , Object obj , Method method ,MethodProxy proxy,Object[] args) throws Exception {
- try{
- begin(dbtype,isReadOnly);
- Object res = method==null? proxy.invokeSuper(obj,args) : method.invoke(obj, args);
- commit(dbtype,isReadOnly);
- return res;
- } catch (Throwable dx) {
- rollback(dbtype,isReadOnly);
- throw new Exception(dx);
- }
- }
- private static void begin(int dbtype , boolean isReadOnly) throws Exception{
- for(TransListener listerner : listenerLst ){
- try{
- listerner.begin(dbtype, isReadOnly);
- }catch(Exception dx){
- log.error("TransListener begin err!" , dx);
- }
- }
- if(isReadOnly){
- DbSessionFactory.beginReadOnlyTransaction(dbtype);
- }else{
- DbSessionFactory.beginTransaction(dbtype);
- }
- }
- private static void commit(int dbtype , boolean isReadOnly) throws Exception{
- if(isReadOnly){
- DbSessionFactory.commitReadOnlyTransaction(dbtype);
- }else{
- DbSessionFactory.commitTransaction(dbtype);
- }
- for(TransListener listerner : listenerLst ){
- try{
- listerner.commit(dbtype, isReadOnly);
- }catch(Exception dx){
- log.error("TransListener commit err!" , dx);
- }
- }
- }
- private static void rollback(int dbtype , boolean isReadOnly) throws Exception{
- if(isReadOnly){
- DbSessionFactory.rollbackReadOnlyTransaction(dbtype);
- }else{
- DbSessionFactory.rollbackTransaction(dbtype);
- }
- for(TransListener listerner : listenerLst ){
- try{
- listerner.rollback(dbtype, isReadOnly);
- }catch(Exception dx){
- log.error("TransListener rollback err!" , dx);
- }
- }
- }
- //------------------------------------
- public interface TransListener {
- public void begin(int dbtype , boolean isReadOnly);
- public void commit(int dbtype , boolean isReadOnly);
- public void rollback(int dbtype , boolean isReadOnly);
- }
- //------------------------------------
- public interface Transaction<T> {
- public T execute() throws Exception;
- }
- }
TransManager
最新推荐文章于 2024-03-08 08:43:49 发布