00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 package org.objectweb.jonas.jdbc_xa;
00028
00029 import java.io.PrintWriter;
00030 import java.sql.Connection;
00031 import java.sql.DriverManager;
00032 import java.sql.SQLException;
00033 import javax.sql.XAConnection;
00034 import javax.sql.XADataSource;
00035 import org.objectweb.jonas.common.Log;
00036 import org.objectweb.util.monolog.api.Logger;
00037 import org.objectweb.util.monolog.api.BasicLevel;
00038
00049 public class XADataSourceImpl implements XADataSource {
00050
00051 private static Logger logger = null;
00052
00053
00054 private String dataSourceName = null;
00055 private String url = null;
00056 private String className = null;
00057 private String userName = null;
00058 private String password = null;
00059 private int isolationLevel = -1;
00060
00061
00062 private int loginTimeout = 60;
00063 private PrintWriter log = null;
00064 private Class driverClass = null;
00065
00066
00067
00068
00069
00073 public XADataSourceImpl() {
00074 }
00075
00076
00077
00078
00079 public String getDataSourceName() {
00080 return dataSourceName;
00081 }
00082 public void setDataSourceName(String s) {
00083 logger = Log.getLogger(Log.JONAS_JDBCXA_PREFIX);
00084 dataSourceName = s;
00085 }
00086
00087 public String getUrl() {
00088 return url;
00089 }
00090 public void setUrl(String s) {
00091 url = s;
00092 }
00093
00094 public String getClassName() {
00095 return className;
00096 }
00097
00098 public void setClassName(String s) throws ClassNotFoundException {
00099 className = s;
00100
00101
00102 logger.log(BasicLevel.DEBUG, "Load JDBC driver " + s);
00103 try {
00104 driverClass = Class.forName(className);
00105 } catch (java.lang.ClassNotFoundException e) {
00106 logger.log(BasicLevel.ERROR, "Cannot load JDBC driver : " + e);
00107 throw e;
00108 }
00109 }
00110
00111 public String getUserName() {
00112 return userName;
00113 }
00114 public void setUserName(String s) {
00115 userName = s;
00116 }
00117
00118 public String getPassword() {
00119 return password;
00120 }
00121 public void setPassword(String s) {
00122 password = s;
00123 }
00124
00125 public void setTransactionIsolation(int level) {
00126 isolationLevel = level;
00127 }
00128
00129
00130
00131
00132
00140 public XAConnection getXAConnection() throws SQLException {
00141 return getXAConnection(userName, password);
00142 }
00143
00144
00156 public XAConnection getXAConnection(String user, String passwd) throws SQLException {
00157
00158
00159 Connection conn = null;
00160 try {
00161 if (user.length() == 0) {
00162 conn = DriverManager.getConnection(url);
00163 logger.log(BasicLevel.DEBUG, " * New Connection on " + url);
00164 } else {
00165
00166 conn = DriverManager.getConnection(url, user, passwd);
00167 logger.log(BasicLevel.DEBUG, " * New Connection on " + url + " for " + user);
00168 }
00169 } catch (SQLException e) {
00170 logger.log(BasicLevel.ERROR, "Could not get Connection on " + url + ":", e);
00171 throw new SQLException("Could not get Connection on url : " + url
00172 + " for user : " + user + " inner exception: " + e.getMessage());
00173 }
00174
00175
00176
00177 if (isolationLevel != -1) {
00178 try {
00179 logger.log(BasicLevel.DEBUG, "set transaction isolation to " + isolationLevel);
00180 conn.setTransactionIsolation(isolationLevel);
00181 } catch (SQLException e) {
00182 String ilstr = "?";
00183 switch (isolationLevel) {
00184 case Connection.TRANSACTION_SERIALIZABLE:
00185 ilstr = "SERIALIZABLE";
00186 break;
00187 case Connection.TRANSACTION_NONE:
00188 ilstr = "NONE";
00189 break;
00190 case Connection.TRANSACTION_READ_COMMITTED:
00191 ilstr = "READ_COMMITTED";
00192 break;
00193 case Connection.TRANSACTION_READ_UNCOMMITTED:
00194 ilstr = "READ_UNCOMMITTED";
00195 break;
00196 case Connection.TRANSACTION_REPEATABLE_READ:
00197 ilstr = "REPEATABLE_READ";
00198 break;
00199 }
00200 logger.log(BasicLevel.ERROR, "Cannot set transaction isolation to " + ilstr + " for this DataSource:");
00201 logger.log(BasicLevel.ERROR, url);
00202 isolationLevel = -1;
00203 }
00204 }
00205
00206
00207 XAConnectionImpl xac = new XAConnectionImpl(conn, this);
00208
00209
00210 return (XAConnection) xac;
00211 }
00212
00220 public PrintWriter getLogWriter() throws SQLException {
00221 return log;
00222 }
00223
00231 public void setLogWriter(PrintWriter out) throws SQLException {
00232 log = out;
00233 }
00234
00235
00244 public void setLoginTimeout(int seconds) throws SQLException {
00245 loginTimeout = seconds;
00246 }
00247
00256 public int getLoginTimeout() throws SQLException {
00257 return loginTimeout;
00258 }
00259
00260 }