1 16 package scriptella.core; 17 18 import scriptella.configuration.ConfigurationException; 19 import scriptella.configuration.ConnectionEl; 20 import scriptella.execution.EtlContext; 21 import scriptella.spi.Connection; 22 import scriptella.spi.ConnectionParameters; 23 import scriptella.spi.ScriptellaDriver; 24 import scriptella.util.UrlPathTokenizer; 25 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 33 34 40 public class ConnectionManager { 41 private static final Logger LOG = Logger.getLogger(ConnectionManager.class.getName()); 42 Connection connection; 43 List <Connection> newConnections; 44 private ScriptellaDriver driver; 45 ConnectionParameters connectionParameters; 46 47 public ConnectionManager(EtlContext ctx, ConnectionEl c) { 48 ClassLoader cl = getClass().getClassLoader(); 50 if (c.getClasspath() != null) { UrlPathTokenizer tok = new UrlPathTokenizer(ctx.getScriptFileURL()); 53 try { 54 URL [] urls = tok.split(c.getClasspath()); 55 if (urls.length > 0) { 56 cl = new DriverClassLoader(urls); 57 } 58 } catch (MalformedURLException e) { 59 throw new ConfigurationException("Unable to parse classpath parameter for " + c, e); 60 } 61 } 62 connectionParameters = new ConnectionParameters(c, ctx); 63 try { 64 driver = DriverFactory.getDriver(c.getDriver(), cl); 65 } catch (ClassNotFoundException e) { 66 throw new ConfigurationException("Driver class " + c.getDriver() + " not found for " + connectionParameters + 67 ".Please check if the class name is correct and required libraries available on classpath", e); 68 } catch (Exception e) { 69 throw new ConfigurationException("Unable to initialize driver for " + connectionParameters + ":" + e.getMessage(), e); 70 } 71 } 72 73 public Connection getConnection() { 74 if (connection == null) { 75 connection = driver.connect(connectionParameters); 76 if (connection == null) { 77 throw new ConfigurationException("Driver returned null connection for " + connectionParameters); 78 } 79 } 80 81 return connection; 82 } 83 84 public Connection newConnection() { 85 final Connection c = driver.connect(connectionParameters); 86 if (c == null) { 87 throw new ConfigurationException("Driver returned null connection for " + connectionParameters); 88 } 89 90 if (newConnections == null) { 91 newConnections = new ArrayList <Connection>(); 92 } 93 94 newConnections.add(c); 95 96 return c; 97 } 98 99 public void rollback() { 100 for (Connection c : getAllConnections()) { 101 try { 102 if (LOG.isLoggable(Level.FINE)) { 103 LOG.fine("Rolling back " + c); 104 } 105 c.rollback(); 106 } catch (UnsupportedOperationException e) { 107 String msg = e.getMessage(); 108 LOG.log(Level.WARNING, 109 "Unable to rollback transaction for connection " + c + (msg == null ? "" : ": " + msg)); 110 111 } catch (Exception e) { 112 LOG.log(Level.WARNING, 113 "Unable to rollback transaction for connection " + c, e); 114 } 115 } 116 } 117 118 public void commit() { 119 for (Connection c : getAllConnections()) { 120 if (LOG.isLoggable(Level.FINE)) { 121 LOG.fine("Commiting connection " + c); 122 } 123 c.commit(); 124 } 125 } 126 127 128 public void close() { 129 for (Connection c : getAllConnections()) { 130 if (c != null) { 131 try { 132 if (LOG.isLoggable(Level.FINE)) { 133 LOG.fine("Closing " + c); 134 } 135 c.close(); 136 } catch (Exception e) { 137 LOG.log(Level.WARNING, "Problem occured while trying to close connection " + c, e); 138 } 139 } 140 } 141 142 connection = null; 143 newConnections = null; 144 connectionParameters = null; 145 driver = null; 146 } 147 148 151 public long getExecutedStatementsCount() { 152 long s = 0; 153 if (connection != null) { 154 s += connection.getExecutedStatementsCount(); 155 156 } 157 if (newConnections != null) { 158 for (Connection c : newConnections) { 159 s += c.getExecutedStatementsCount(); 160 } 161 } 162 return s; 163 } 164 165 168 private List <Connection> getAllConnections() { 169 List <Connection> cl = new ArrayList <Connection>(); 170 171 if (newConnections != null) { 172 cl.addAll(newConnections); 173 } 174 175 if (connection != null) { 176 cl.add(connection); 177 } 178 return cl; 179 } 180 181 182 } 183 | Popular Tags |