1 3 package jodd.db.connection; 4 5 import jodd.db.DbSqlException; 6 7 import javax.sql.XADataSource ; 8 import javax.sql.XAConnection ; 9 import java.sql.Connection ; 10 import java.sql.SQLException ; 11 12 15 public class XADataSourceConnectionProvider implements ConnectionProvider { 16 17 private XADataSource xaDataSource; 18 private String username; 19 private String password; 20 21 public XADataSourceConnectionProvider(XADataSource dataSource, String user, String pass) { 22 this.xaDataSource = dataSource; 23 this.username = user; 24 this.password = pass; 25 } 26 27 public void init() {} 28 29 public Connection getConnection() { 30 try { 31 XAConnection xaConnection; 32 if (username != null) { 33 xaConnection = xaDataSource.getXAConnection(username, password); 34 } else { 35 xaConnection = xaDataSource.getXAConnection(); 36 } 37 return xaConnection.getConnection(); 38 } catch (SQLException sex) { 39 throw new DbSqlException("Unable to get connection from XA datasource.", sex); 40 } 41 } 42 43 public void closeConnection(Connection conn) { 44 try { 45 conn.close(); 46 } catch (SQLException sex) { 47 } 49 } 50 51 public void close() {} 52 } 53 | Popular Tags |