View Javadoc
1 2 package net.sf.voruta; 3 4 import java.util.*; 5 import java.sql.*; 6 import net.sf.cglib.*; 7 import java.lang.reflect.*; 8 import java.lang.ref.*; 9 /*** 10 * 11 * @author baliuka 12 */ 13 final public class ThreadLocalConnection { 14 15 16 private static final Map connections = Collections.synchronizedMap( new HashMap() ); 17 private static final Map cacheRegions = Collections.synchronizedMap( new HashMap() ); 18 19 /* 20 private static final List closeCallbacks = Collections.synchronizedList( new ArrayList()); 21 22 public interface CloseCallback{ 23 24 void onClose(String connection); 25 26 }; 27 28 public static synchronized void registerCloseCallback( CloseCallback callback ){ 29 30 for(int i = 0; i < closeCallbacks.size(); i++ ){ 31 if(closeCallbacks.get(i) == null || 32 ((WeakReference)closeCallbacks.get(i)).get() == null ){ 33 closeCallbacks.set(i,new WeakReference(callback)); 34 return; 35 } 36 } 37 closeCallbacks.add(new WeakReference(callback)); 38 } 39 40 static synchronized void fireOnClose(String connection){ 41 42 for(int i = 0; i < closeCallbacks.size(); i++ ){ 43 44 WeakReference ref = (WeakReference)closeCallbacks.get(i); 45 46 if( ref != null ){ 47 48 CloseCallback callback = (CloseCallback)ref.get(); 49 50 if(callback != null){ 51 callback.onClose(connection); 52 } 53 54 } 55 56 } 57 58 } 59 */ 60 61 private static ConnectionFactory factory; 62 63 private ThreadLocalConnection() { 64 } 65 66 public static synchronized void init(ConnectionFactory factory){ 67 ThreadLocalConnection.factory = factory; 68 } 69 70 public static Connection get(String name)throws Exception{ 71 72 ThreadLocal local; 73 74 synchronized(connections){ 75 76 local = (ThreadLocal)connections.get(name); 77 if(local == null){ 78 local = new ThreadLocal(); 79 connections.put(name,local); 80 81 } 82 } 83 84 Connection connection = (Connection)local.get(); 85 86 if( connection == null || connection.isClosed() ){ 87 connection = decorate(factory,name); 88 local.set(connection); 89 getRegions(name).beforeBegin(); 90 } 91 92 return connection; 93 } 94 95 static Connection getConnection(String name){ 96 97 ThreadLocal local = (ThreadLocal)connections.get(name); 98 if(local != null){ 99 return (Connection)local.get(); 100 } 101 return null; 102 } 103 104 static void commit(String name){ 105 try{ 106 Connection c = getConnection(name); 107 if(c != null){ 108 c.commit(); 109 getRegions(name).afterCommit(); 110 } 111 }catch(SQLException sqle){ 112 throw new DbException(sqle); 113 } 114 } 115 116 static void rollback(String name){ 117 try{ 118 Connection c = getConnection(name); 119 if(c != null){ 120 c.rollback(); 121 getRegions(name).afterRollback(); 122 } 123 }catch(SQLException sqle){ 124 throw new DbException(sqle); 125 } 126 } 127 128 129 static void close(String name){ 130 try{ 131 ThreadLocal local = (ThreadLocal)connections.get(name); 132 if(local != null){ 133 Connection c = (Connection)local.get(); 134 if(c != null){ 135 try{ 136 SQLException warning = c.getWarnings(); 137 while( warning != null ){ 138 DbUtils.getLog().warn(warning); 139 warning = warning.getNextException(); 140 } 141 c.clearWarnings(); 142 }catch(Throwable t){ 143 DbUtils.getLog().warn(t); 144 } 145 local.set(null); 146 c.close(); 147 } 148 } 149 150 }catch(SQLException sqle){ 151 throw new DbException(sqle); 152 } 153 } 154 155 156 157 private static Connection decorate(ConnectionFactory factory, String configName) throws Exception{ 158 159 if(factory == null){ 160 throw new DbException("no factory for connection is configured"); 161 } 162 163 return factory.newInstance(configName); 164 165 166 167 } 168 169 170 static CacheRegions getRegions(String config){ 171 CacheRegions regions = (CacheRegions)cacheRegions.get(config); 172 if( regions == null ){ 173 int isolation ; 174 try{ 175 176 java.sql.Connection connection = ThreadLocalConnection.get(config); 177 if(connection.getAutoCommit()){ 178 isolation = java.sql.Connection.TRANSACTION_NONE; 179 }else{ 180 isolation = connection.getTransactionIsolation(); 181 } 182 183 }catch(Exception sqle){ 184 throw new DbException(sqle); 185 186 } 187 regions = new CacheRegions(isolation); 188 cacheRegions.put(config,regions); 189 } 190 return regions; 191 } 192 193 194 195 }

This page was automatically generated by Maven