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