KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > connection > DataSourceConnectionProvider


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.db.connection;
4
5 import jodd.db.DbSqlException;
6
7 import javax.sql.DataSource JavaDoc;
8 import java.sql.Connection JavaDoc;
9 import java.sql.SQLException JavaDoc;
10
11 /**
12  * DataSource connection provider.
13  */

14 public class DataSourceConnectionProvider implements ConnectionProvider {
15
16     private DataSource JavaDoc dataSource;
17     private String JavaDoc username;
18     private String JavaDoc password;
19
20     public DataSourceConnectionProvider(DataSource JavaDoc dataSource, String JavaDoc user, String JavaDoc pass) {
21         this.dataSource = dataSource;
22         this.username = user;
23         this.password = pass;
24     }
25
26     public void init() {}
27
28     public Connection JavaDoc 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 JavaDoc sex) {
36             throw new DbSqlException("Unable to get connection from datasource.", sex);
37         }
38     }
39
40     public void closeConnection(Connection JavaDoc conn) {
41         try {
42             conn.close();
43         } catch (SQLException JavaDoc sex) {
44             // ignore
45
}
46     }
47
48     public void close() {}
49 }
50
Popular Tags