1 22 package org.jboss.resource.adapter.jdbc; 23 24 import java.io.PrintWriter ; 25 import java.io.Serializable ; 26 import java.sql.Connection ; 27 import java.sql.SQLException ; 28 29 import javax.naming.Reference ; 30 import javax.resource.Referenceable ; 31 import javax.resource.ResourceException ; 32 import javax.resource.spi.ConnectionManager ; 33 import javax.resource.spi.ConnectionRequestInfo ; 34 import javax.sql.DataSource ; 35 import javax.transaction.RollbackException ; 36 37 import org.jboss.tm.TransactionTimeoutConfiguration; 38 import org.jboss.util.NestedSQLException; 39 40 47 public class WrapperDataSource implements Referenceable , DataSource , Serializable 48 { 49 static final long serialVersionUID = 3570285419164793501L; 50 51 private final BaseWrapperManagedConnectionFactory mcf; 52 private final ConnectionManager cm; 53 54 private Reference reference; 55 56 public WrapperDataSource (final BaseWrapperManagedConnectionFactory mcf, final ConnectionManager cm) 57 { 58 this.mcf = mcf; 59 this.cm = cm; 60 } 61 62 public PrintWriter getLogWriter() throws SQLException 63 { 64 return null; 66 } 67 68 public void setLogWriter(PrintWriter param1) throws SQLException 69 { 70 } 72 73 public int getLoginTimeout() throws SQLException 74 { 75 return 0; 77 } 78 79 public void setLoginTimeout(int param1) throws SQLException 80 { 81 } 83 84 public Connection getConnection() throws SQLException 85 { 86 try 87 { 88 WrappedConnection wc = (WrappedConnection) cm.allocateConnection(mcf, null); 89 wc.setDataSource(this); 90 return wc; 91 } 92 catch (ResourceException re) 93 { 94 throw new NestedSQLException(re); 95 } 96 } 97 98 public Connection getConnection(String user, String password) throws SQLException 99 { 100 ConnectionRequestInfo cri = new WrappedConnectionRequestInfo(user, password); 101 try 102 { 103 WrappedConnection wc = (WrappedConnection) cm.allocateConnection(mcf, cri); 104 wc.setDataSource(this); 105 return wc; 106 } 107 catch (ResourceException re) 108 { 109 throw new NestedSQLException(re); 110 } 111 } 112 113 public void setReference(final Reference reference) 114 { 115 this.reference = reference; 116 } 117 118 public Reference getReference() 119 { 120 return reference; 121 } 122 123 protected int getTimeLeftBeforeTransactionTimeout() throws SQLException 124 { 125 try 126 { 127 if (cm instanceof TransactionTimeoutConfiguration) 128 { 129 long timeout = ((TransactionTimeoutConfiguration) cm).getTimeLeftBeforeTransactionTimeout(true); 130 if (timeout == -1) 132 return -1; 133 long result = timeout / 1000; 135 if ((result % 1000) != 0) 136 ++result; 137 return (int) result; 138 } 139 else 140 return -1; 141 } 142 catch (RollbackException e) 143 { 144 throw new NestedSQLException(e); 145 } 146 } 147 } 148 | Popular Tags |