Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 239   Methods: 17
NCLOC: 191   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NonManagedConnectionFactory.java 58.3% 76.2% 94.1% 75%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.loader;
 8   
 9    import org.apache.commons.logging.Log;
 10    import org.apache.commons.logging.LogFactory;
 11   
 12    import java.sql.Connection;
 13    import java.sql.DriverManager;
 14    import java.sql.SQLException;
 15   
 16    /**
 17    * Standard connection factory for standalone JBossCache implementations without connection pooling capabilities.
 18    *
 19    * @author <a href="mailto:hmesha@novell.com">Hany Mesha </a>
 20    * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
 21    */
 22    class NonManagedConnectionFactory implements ConnectionFactory
 23    {
 24    private static final Log log = LogFactory.getLog(NonManagedConnectionFactory.class);
 25   
 26    private static final ThreadLocal connection = new ThreadLocal();
 27   
 28    private String url;
 29    private String usr;
 30    private String pwd;
 31    private String driverClass;
 32   
 33  198 public void setConfig(AdjListJDBCCacheLoaderConfig config)
 34    {
 35  198 url = config.getJdbcURL();
 36  198 usr = config.getJdbcUser();
 37  198 pwd = config.getJdbcPassword();
 38  198 driverClass = config.getDriverClass();
 39    }
 40   
 41  192 public void start() throws Exception
 42    {
 43  192 loadDriver(driverClass);
 44    }
 45   
 46  15 public void prepare(Object tx)
 47    {
 48  15 Connection con = getConnection();
 49  15 try
 50    {
 51  15 if (con.getAutoCommit())
 52    {
 53  15 con.setAutoCommit(false);
 54    }
 55    }
 56    catch (Exception e)
 57    {
 58  0 reportAndRethrowError("Failed to set auto-commit", e);
 59    }
 60   
 61    /* Connection set in ThreadLocal, no reason to return. It was previously returned for legacy purpouses
 62    and to trace log the connection opening in JDBCCacheLoader. */
 63  15 connection.set(con);
 64   
 65  15 if (log.isTraceEnabled())
 66    {
 67  0 log.trace("opened tx connection: tx=" + tx + ", con=" + con);
 68    }
 69   
 70    }
 71   
 72  42349 public Connection getConnection()
 73    {
 74  42349 Connection con = (Connection) connection.get();
 75   
 76  42349 if (con == null)
 77    {
 78  42237 try
 79    {
 80  42237 con = checkoutConnection();
 81    // connection.set(con);
 82    }
 83    catch (SQLException e)
 84    {
 85  1 reportAndRethrowError("Failed to get connection for url=" + url + ", user=" + usr + ", password=" + pwd, e);
 86    }
 87    }
 88   
 89  42348 if (log.isTraceEnabled())
 90    {
 91  0 log.trace("using connection: " + con);
 92    }
 93   
 94  42348 return con;
 95    }
 96   
 97  22280 public Connection checkoutConnection() throws SQLException
 98    {
 99  22280 return DriverManager.getConnection(url, usr, pwd);
 100    }
 101   
 102  10 public void commit(Object tx)
 103    {
 104  10 Connection con = (Connection) connection.get();
 105  10 if (con == null)
 106    {
 107  0 throw new IllegalStateException("Failed to commit: thread is not associated with the connection!");
 108    }
 109   
 110  10 try
 111    {
 112  10 con.commit();
 113  10 if (log.isTraceEnabled())
 114    {
 115  0 log.trace("committed tx=" + tx + ", con=" + con);
 116    }
 117    }
 118    catch (SQLException e)
 119    {
 120  0 reportAndRethrowError("Failed to commit", e);
 121    }
 122    finally
 123    {
 124  10 closeTxConnection(con);
 125    }
 126    }
 127   
 128  5 public void rollback(Object tx)
 129    {
 130  5 Connection con = (Connection) connection.get();
 131  5 if (con == null)
 132    {
 133    // todo: prepare was not called. why is rollback called?
 134  0 throw new IllegalStateException("Failed to rollback: thread is not associated with the connection!");
 135    }
 136   
 137  5 try
 138    {
 139  5 con.rollback();
 140  5 if (log.isTraceEnabled())
 141    {
 142  0 log.trace("rolledback tx=" + tx + ", con=" + con);
 143    }
 144    }
 145    catch (SQLException e)
 146    {
 147  0 reportAndRethrowError("Failed to rollback", e);
 148    }
 149    finally
 150    {
 151  5 closeTxConnection(con);
 152    }
 153    }
 154   
 155  42334 public void close(Connection con)
 156    {
 157  42334 if (con != null && con != connection.get())
 158    {
 159  42221 try
 160    {
 161  42221 con.close();
 162   
 163  42221 if (log.isTraceEnabled())
 164    {
 165  0 log.trace("closed non tx connection: " + con);
 166    }
 167    }
 168    catch (SQLException e)
 169    {
 170  0 log.warn("Failed to close connection " + con , e);
 171    }
 172    }
 173    }
 174   
 175  127 public void stop()
 176    {
 177    }
 178   
 179  65 public String getUrl()
 180    {
 181  65 return url;
 182    }
 183   
 184  65 public String getUsr()
 185    {
 186  65 return usr;
 187    }
 188   
 189  65 public String getPwd()
 190    {
 191  65 return pwd;
 192    }
 193   
 194  0 public String getDriverClass()
 195    {
 196  0 return driverClass;
 197    }
 198   
 199  192 protected void loadDriver(String drv)
 200    {
 201  192 try
 202    {
 203  0 if (log.isTraceEnabled()) { log.trace("Attempting to load driver: " + drv); }
 204  192 Class.forName(drv).newInstance();
 205    }
 206    catch (Exception e)
 207    {
 208  0 reportAndRethrowError("Failed to load driver " + drv, e);
 209    }
 210    }
 211   
 212  15 private void closeTxConnection(Connection con)
 213    {
 214  15 safeClose(con);
 215  15 connection.set(null);
 216    }
 217   
 218  15 private void safeClose(Connection con)
 219    {
 220  15 if (con != null)
 221    {
 222  15 try
 223    {
 224  15 con.close();
 225    }
 226    catch (SQLException e)
 227    {
 228  0 log.warn("Failed to close connection", e);
 229    }
 230    }
 231    }
 232   
 233  1 private void reportAndRethrowError(String message, Exception cause) throws IllegalStateException
 234    {
 235  1 log.error(message, cause);
 236  1 throw new IllegalStateException(message, cause);
 237    }
 238    }
 239