1 25 26 27 package org.objectweb.jonas.jdbc_xa; 28 29 import java.io.PrintWriter ; 30 import java.sql.Connection ; 31 import java.sql.DriverManager ; 32 import java.sql.SQLException ; 33 import javax.sql.XAConnection ; 34 import javax.sql.XADataSource ; 35 import org.objectweb.jonas.common.Log; 36 import org.objectweb.util.monolog.api.Logger; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 49 public class XADataSourceImpl implements XADataSource { 50 51 private static Logger logger = null; 52 53 private String dataSourceName = null; 55 private String url = null; 56 private String className = null; 57 private String userName = null; 58 private String password = null; 59 private int isolationLevel = -1; 60 61 private int loginTimeout = 60; 63 private PrintWriter log = null; 64 private Class driverClass = null; 65 66 70 73 public XADataSourceImpl() { 74 } 75 76 public String getDataSourceName() { 80 return dataSourceName; 81 } 82 public void setDataSourceName(String s) { 83 logger = Log.getLogger(Log.JONAS_JDBCXA_PREFIX); 84 dataSourceName = s; 85 } 86 87 public String getUrl() { 88 return url; 89 } 90 public void setUrl(String s) { 91 url = s; 92 } 93 94 public String getClassName() { 95 return className; 96 } 97 98 public void setClassName(String s) throws ClassNotFoundException { 99 className = s; 100 101 if (logger.isLoggable(BasicLevel.DEBUG)) { 103 logger.log(BasicLevel.DEBUG, "Load JDBC driver " + s); 104 } 105 try { 106 driverClass = Class.forName(className); 107 } catch (java.lang.ClassNotFoundException e) { 108 logger.log(BasicLevel.ERROR, "Cannot load JDBC driver : " + e); 109 throw e; 110 } 111 } 112 113 public String getUserName() { 114 return userName; 115 } 116 public void setUserName(String s) { 117 userName = s; 118 } 119 120 public String getPassword() { 121 return password; 122 } 123 public void setPassword(String s) { 124 password = s; 125 } 126 127 public void setTransactionIsolation(int level) { 128 isolationLevel = level; 129 } 130 131 135 142 public XAConnection getXAConnection() throws SQLException { 143 return getXAConnection(userName, password); 144 } 145 146 147 158 public XAConnection getXAConnection(String user, String passwd) throws SQLException { 159 160 Connection conn = null; 162 try { 163 if (user.length() == 0) { 164 conn = DriverManager.getConnection(url); 165 if (logger.isLoggable(BasicLevel.DEBUG)) { 166 logger.log(BasicLevel.DEBUG, " * New Connection on " + url); 167 } 168 } else { 169 conn = DriverManager.getConnection(url, user, passwd); 171 if (logger.isLoggable(BasicLevel.DEBUG)) { 172 logger.log(BasicLevel.DEBUG, " * New Connection on " + url + " for " + user); 173 } 174 } 175 } catch (SQLException e) { 176 logger.log(BasicLevel.ERROR, "Could not get Connection on " + url + ":", e); 177 throw new SQLException ("Could not get Connection on url : " + url 178 + " for user : " + user + " inner exception" + e.getMessage()); 179 } 180 181 if (isolationLevel != -1) { 184 try { 185 if (logger.isLoggable(BasicLevel.DEBUG)) { 186 logger.log(BasicLevel.DEBUG, "set transaction isolation to " + isolationLevel); 187 } 188 conn.setTransactionIsolation(isolationLevel); 189 } catch (SQLException e) { 190 String ilstr = "?"; 191 switch (isolationLevel) { 192 case Connection.TRANSACTION_SERIALIZABLE: 193 ilstr = "SERIALIZABLE"; 194 break; 195 case Connection.TRANSACTION_NONE: 196 ilstr = "NONE"; 197 break; 198 case Connection.TRANSACTION_READ_COMMITTED: 199 ilstr = "READ_COMMITTED"; 200 break; 201 case Connection.TRANSACTION_READ_UNCOMMITTED: 202 ilstr = "READ_UNCOMMITTED"; 203 break; 204 case Connection.TRANSACTION_REPEATABLE_READ: 205 ilstr = "REPEATABLE_READ"; 206 break; 207 } 208 logger.log(BasicLevel.ERROR, "Cannot set transaction isolation to " + ilstr + " for this DataSource:" + e); 209 logger.log(BasicLevel.ERROR, url); 210 isolationLevel = -1; 211 } 212 } 213 214 XAConnectionImpl xac = new XAConnectionImpl(conn, this); 216 217 return (XAConnection ) xac; 219 } 220 221 228 public PrintWriter getLogWriter() throws SQLException { 229 return log; 230 } 231 232 239 public void setLogWriter(PrintWriter out) throws SQLException { 240 log = out; 241 } 242 243 244 252 public void setLoginTimeout(int seconds) throws SQLException { 253 loginTimeout = seconds; 254 } 255 256 264 public int getLoginTimeout() throws SQLException { 265 return loginTimeout; 266 } 267 268 } 269 | Popular Tags |