1 5 package org.h2.jdbcx; 6 7 import java.io.PrintWriter ; 8 import java.io.Serializable ; 9 import java.sql.Connection ; 10 import java.sql.SQLException ; 11 import java.util.Properties ; 12 13 import javax.naming.NamingException ; 14 import javax.naming.Reference ; 15 import javax.naming.Referenceable ; 16 import javax.naming.StringRefAddr ; 17 import javax.sql.ConnectionPoolDataSource ; 19 import javax.sql.DataSource ; 20 import javax.sql.PooledConnection ; 21 import javax.sql.XAConnection ; 22 import javax.sql.XADataSource ; 23 25 import org.h2.jdbc.JdbcConnection; 26 import org.h2.message.TraceObject; 27 28 32 34 39 public class JdbcDataSource extends TraceObject implements 40 XADataSource , DataSource , ConnectionPoolDataSource , 42 Serializable , Referenceable { 44 45 private static final long serialVersionUID = 1288136338451857771L; 46 47 private transient JdbcDataSourceFactory factory = new JdbcDataSourceFactory(); 48 private transient PrintWriter logWriter; 49 private int timeout; 50 private String user = ""; 51 private String password = ""; 52 private String url = ""; 53 54 public JdbcDataSource() { 55 int id = getNextId(TraceObject.DATASOURCE); 56 setTrace(factory.getTrace(), TraceObject.DATASOURCE, id); 57 } 58 59 public int getLoginTimeout() throws SQLException { 60 debugCodeCall("getLoginTimeout"); 61 return timeout; 62 } 63 64 public void setLoginTimeout(int timeout) throws SQLException { 65 debugCodeCall("setLoginTimeout", timeout); 66 this.timeout = timeout; 67 } 68 69 public PrintWriter getLogWriter() throws SQLException { 70 debugCodeCall("getLogWriter"); 71 return logWriter; 72 } 73 74 public void setLogWriter(PrintWriter out) throws SQLException { 75 debugCodeCall("setLogWriter(out)"); 76 logWriter = out; 77 } 78 79 public Connection getConnection() throws SQLException { 80 debugCodeCall("getConnection"); 81 return getJdbcConnection(user, password); 82 } 83 84 public Connection getConnection(String user, String password) throws SQLException { 85 debugCode("getConnection("+quote(user)+", "+quote(password)+");"); 86 return getJdbcConnection(user, password); 87 } 88 89 public JdbcConnection getJdbcConnection(String user, String password) throws SQLException { 90 debugCode("getJdbcConnection("+quote(user)+", "+quote(password)+");"); 91 Properties info = new Properties (); 92 info.setProperty("user", user); 93 info.setProperty("password", password); 94 return new JdbcConnection(url, info); 95 } 96 97 public String getURL() { 98 debugCodeCall("getURL"); 99 return url; 100 } 101 102 public void setURL(String url) { 103 debugCodeCall("setURL", url); 104 this.url = url; 105 } 106 107 public String getPassword() { 108 debugCodeCall("getPassword"); 109 return password; 110 } 111 112 public void setPassword(String password) { 113 debugCodeCall("setPassword", password); 114 this.password = password; 115 } 116 117 public String getUser() { 118 debugCodeCall("getUser"); 119 return user; 120 } 121 122 public void setUser(String user) { 123 debugCodeCall("setUser", user); 124 this.user = user; 125 } 126 127 128 public Reference getReference() throws NamingException { 129 debugCodeCall("getReference"); 130 String factoryClassName = JdbcDataSourceFactory.class.getName(); 131 Reference ref = new Reference (getClass().getName(), factoryClassName, null); 132 ref.add(new StringRefAddr ("url", getURL())); 133 ref.add(new StringRefAddr ("user", getUser())); 134 ref.add(new StringRefAddr ("password", password)); 135 return ref; 136 } 137 138 public XAConnection getXAConnection() throws SQLException { 140 debugCodeCall("getXAConnection"); 141 int id = getNextId(XA_DATASOURCE); 142 return new JdbcXAConnection(factory, id, url, user, password); 143 } 144 145 public XAConnection getXAConnection(String user, String password) throws SQLException { 146 debugCode("getXAConnection("+quote(user)+", "+quote(password)+");"); 147 int id = getNextId(XA_DATASOURCE); 148 return new JdbcXAConnection(factory, id, url, user, password); 149 } 150 151 public PooledConnection getPooledConnection() throws SQLException { 152 debugCodeCall("getPooledConnection"); 153 return getXAConnection(); 154 } 155 156 public PooledConnection getPooledConnection(String user, String password) throws SQLException { 157 debugCode("getPooledConnection("+quote(user)+", "+quote(password)+");"); 158 return getXAConnection(user, password); 159 } 160 162 168 170 176 178 } 179 | Popular Tags |