KickJava   Java API By Example, From Geeks To Geeks.

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


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.XADataSource JavaDoc;
8 import javax.sql.XAConnection JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.sql.SQLException JavaDoc;
11
12 /**
13  * DataSource connection provider.
14  */

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