1 4 package org.ofbiz.minerva.pool.jdbc.xa.wrapper; 5 6 import javax.sql.XAConnection ; 7 import javax.sql.XADataSource ; 8 import java.io.PrintWriter ; 9 import java.sql.Connection ; 10 import java.sql.Driver ; 11 import java.sql.DriverManager ; 12 import java.sql.SQLException ; 13 import java.util.Properties ; 14 15 import org.apache.log4j.Logger; 16 17 27 public class XADataSourceImpl implements XADataSource { 28 29 private String url; 30 private String user; 31 private String password; 32 private String driverName; 33 private Driver driver; 34 private Properties properties; 35 private int loginTimeout; 36 private PrintWriter logWriter; 37 private boolean saveStackTrace; 38 private static Logger log = Logger.getLogger(XADataSourceImpl.class); 39 40 43 public XADataSourceImpl() { 44 } 45 46 50 public XADataSourceImpl(String url, Properties properties) { 51 this.url = url; 52 this.properties = properties; 53 } 54 55 58 public String getURL() { 59 return url; 60 } 61 62 65 public void setURL(String url) { 66 this.url = url; 67 } 68 69 72 public String getUser() { 73 return user; 74 } 75 76 80 public void setUser(String user) { 81 this.user = user; 82 } 83 84 87 public String getPassword() { 88 return password; 89 } 90 91 95 public void setPassword(String password) { 96 this.password = password; 97 } 98 99 public void setDriver(String driverName) { 100 this.driverName = driverName; 101 } 102 103 106 public Properties getProperties() { 107 return properties; 108 } 109 110 114 public void setProperties(Properties properties) { 115 this.properties = properties; 116 } 117 118 123 public boolean getSaveStackTrace() { 124 return saveStackTrace; 125 } 126 127 public void setSaveStackTrace(boolean save) { 128 saveStackTrace = save; 129 } 130 131 134 public PrintWriter getLogWriter() throws SQLException { 135 return logWriter; 136 } 137 138 141 public void setLogWriter(PrintWriter writer) throws SQLException { 142 if (writer == null) { 143 logWriter = null; 144 } 145 146 } 147 148 152 public int getLoginTimeout() throws SQLException { 153 return loginTimeout; 154 } 155 156 160 public void setLoginTimeout(int timeout) throws SQLException { 161 loginTimeout = timeout; 162 } 163 164 private void loadDriver() throws SQLException { 165 if (driver == null) { 166 try { 167 driver = (Driver ) Class.forName(driverName, true, Thread.currentThread().getContextClassLoader()).newInstance(); 168 DriverManager.registerDriver(driver); 169 } catch (ClassNotFoundException e) { 170 log.warn("unable to load driver", e); 171 } catch (InstantiationException e) { 172 log.warn("unable to instantiate driver", e); 173 } catch (IllegalAccessException e) { 174 log.warn("illegal access exception", e); 175 } 176 } 177 } 178 179 185 public XAConnection getXAConnection() throws SQLException { 186 187 loadDriver(); 188 189 Connection con; 190 if (user != null && user.length() > 0) 191 con = DriverManager.getConnection(url, user, password); 192 else if (properties != null) 193 con = DriverManager.getConnection(url, properties); 194 else 195 con = DriverManager.getConnection(url); 196 197 198 try { 199 con.setAutoCommit(false); 200 } catch (SQLException e) { 201 log.warn("Unable to disable auto-commit on " + con.getClass().getName()); 202 } 203 204 XAResourceImpl res = new XAResourceImpl(con); 205 XAConnectionImpl xacon = new XAConnectionImpl(con, res, saveStackTrace); 206 res.setXAConnection(xacon); 207 208 209 log.debug("created new Connection(" + con.getClass().getName() + ") with XAResource " + res.getClass().getName() + " and XAConnection " + xacon.getClass().getName() + "."); 210 211 return xacon; 212 } 213 214 220 public XAConnection getXAConnection(String user, String password) throws SQLException { 221 222 loadDriver(); 223 Connection con = DriverManager.getConnection(url, user, password); 224 225 try { 226 con.setAutoCommit(false); 227 } catch (SQLException e) { 228 if (logWriter != null) 229 logWriter.println("XADataSource unable to disable auto-commit on " + con.getClass().getName()); 230 } 231 232 XAResourceImpl res = new XAResourceImpl(con); 233 XAConnectionImpl xacon = new XAConnectionImpl(con, res, saveStackTrace); 234 res.setXAConnection(xacon); 235 236 xacon.setUser(user); 237 xacon.setPassword(password); 238 239 log.debug(" created new Connection (" + con.getClass().getName() + ") with XAResource " + res.getClass().getName() + " and XAConnection with userid and password " + xacon.getClass().getName()); 240 241 242 return xacon; 243 } 244 } 245 | Popular Tags |