1 23 24 package com.sun.gjc.spi; 25 26 import java.io.PrintWriter ; 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import javax.resource.spi.ManagedConnectionFactory ; 30 import javax.resource.spi.ConnectionManager ; 31 import javax.resource.ResourceException ; 32 import javax.naming.Reference ; 33 import com.sun.gjc.spi.ConnectionHolder.ConnectionType; 34 35 import com.sun.logging.*; 36 import java.util.logging.Logger ; 37 import java.util.logging.Level ; 38 import com.sun.enterprise.connectors.ConnectionManagerImpl; 39 import com.sun.enterprise.connectors.ConnectorConstants; 40 41 48 public class DataSource implements javax.sql.DataSource , java.io.Serializable , 49 com.sun.appserv.jdbc.DataSource, javax.resource.Referenceable { 50 51 private ManagedConnectionFactory mcf; 52 private ConnectionManager cm; 53 private int loginTimeout; 54 private PrintWriter logWriter; 55 private String description; 56 private Reference reference; 57 58 private ConnectionType conType_; 59 60 private static Logger _logger; 61 static { 62 _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER ); 63 } 64 private boolean debug = false; 65 66 75 public DataSource (ManagedConnectionFactory mcf, ConnectionManager cm) { 76 this.mcf = mcf; 77 if (cm == null) { 78 this.cm = (ConnectionManager) new com.sun.gjc.spi.ConnectionManager(); 79 } else { 80 this.cm = cm; 81 conType_ = findConnectionType(); 82 } 83 } 84 85 91 public Connection getConnection() throws SQLException { 92 try { 93 ConnectionHolder con = (ConnectionHolder) 94 cm.allocateConnection( mcf, null ); 95 setConnectionType( con ); 96 97 return (Connection ) con; 98 } catch (ResourceException re) { 99 _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage()); 100 throw new SQLException (re.getMessage()); 101 } 102 } 103 104 112 public Connection getConnection(String user, String pwd) throws SQLException { 113 try { 114 ConnectionRequestInfo info = new ConnectionRequestInfo (user, pwd); 115 ConnectionHolder con = (ConnectionHolder) 116 cm.allocateConnection(mcf,info); 117 setConnectionType( con ); 118 return (Connection ) con; 119 } catch (ResourceException re) { 120 _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage()); 121 throw new SQLException (re.getMessage()); 122 } 123 } 124 125 134 public Connection getConnection(Connection con) throws SQLException { 135 136 Connection driverCon = con; 137 if (con instanceof com.sun.gjc.spi.ConnectionHolder) { 138 driverCon = ((com.sun.gjc.spi.ConnectionHolder) con).getConnection(); 139 } 140 141 return driverCon; 142 } 143 144 154 public Connection getNonTxConnection() throws SQLException { 155 try { 156 ConnectionHolder con = (ConnectionHolder) 157 ((com.sun.enterprise.connectors.ConnectionManagerImpl) 158 cm).allocateNonTxConnection(mcf, null); 159 setConnectionType( con, true ); 160 161 return (Connection ) con; 162 } catch( ResourceException re ) { 163 _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage()); 164 throw new SQLException ( re.getMessage() ); 165 } 166 } 167 168 180 public Connection getNonTxConnection(String user, String password) throws SQLException { 181 try { 182 ConnectionRequestInfo cxReqInfo = new ConnectionRequestInfo(user, password); 183 ConnectionHolder con = (ConnectionHolder) 184 ((com.sun.enterprise.connectors.ConnectionManagerImpl) 185 cm).allocateNonTxConnection( mcf, cxReqInfo); 186 187 setConnectionType( con, true ); 188 189 return (Connection ) con; 190 } catch( ResourceException re ) { 191 _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage()); 192 throw new SQLException ( re.getMessage() ); 193 } 194 } 195 196 202 public int getLoginTimeout() throws SQLException { 203 return loginTimeout; 204 } 205 206 212 public void setLoginTimeout(int loginTimeout) throws SQLException { 213 this.loginTimeout = loginTimeout; 214 } 215 216 222 public PrintWriter getLogWriter() throws SQLException { 223 return logWriter; 224 } 225 226 232 public void setLogWriter(PrintWriter logWriter) throws SQLException { 233 this.logWriter = logWriter; 234 } 235 236 241 public String getDescription() { 242 return description; 243 } 244 245 250 public void setDescription(String description) { 251 this.description = description; 252 } 253 254 259 public Reference getReference() { 260 return reference; 261 } 262 263 268 public void setReference(Reference reference) { 269 this.reference = reference; 270 } 271 272 private ConnectionType findConnectionType() { 273 ConnectionType cmType = ConnectionType.STANDARD; 274 275 if ( cm instanceof javax.resource.spi.LazyAssociatableConnectionManager ) { 276 if (! ((com.sun.enterprise.connectors.ConnectionManagerImpl)cm). 277 getJndiName().endsWith( ConnectorConstants.PM_JNDI_SUFFIX ) ) { 278 cmType = ConnectionType.LAZY_ASSOCIATABLE; 279 } 280 } else if ( cm instanceof 281 javax.resource.spi.LazyEnlistableConnectionManager ) { 282 if (! ((com.sun.enterprise.connectors.ConnectionManagerImpl)cm). 283 getJndiName().endsWith( ConnectorConstants.PM_JNDI_SUFFIX) && 284 ! ((com.sun.enterprise.connectors.ConnectionManagerImpl)cm). 285 getJndiName().endsWith( ConnectorConstants.NON_TX_JNDI_SUFFIX)) { 286 cmType = ConnectionType.LAZY_ENLISTABLE; 287 } 288 } 289 290 return cmType; 291 } 292 293 private void setConnectionType( ConnectionHolder con ) { 294 this.setConnectionType( con, false); 295 } 296 297 private void setConnectionType( ConnectionHolder con, boolean isNonTx ) { 298 con.setConnectionType( conType_ ); 299 if ( conType_ == ConnectionType.LAZY_ASSOCIATABLE ) { 300 con.setLazyAssociatableConnectionManager( 301 (javax.resource.spi.LazyAssociatableConnectionManager ) cm); 302 } else if (conType_ == ConnectionType.LAZY_ENLISTABLE) { 303 if ( isNonTx ) { 304 con.setConnectionType( ConnectionType.STANDARD ); 307 } else { 308 con.setLazyEnlistableConnectionManager( 309 (javax.resource.spi.LazyEnlistableConnectionManager ) cm ); 310 } 311 } 312 } 313 314 } 315 | Popular Tags |