KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DatabaseHelper


1
2 import java.sql.Connection JavaDoc;
3 import java.sql.SQLException JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 import javax.naming.InitialContext JavaDoc;
7 import javax.sql.XAConnection JavaDoc;
8 import javax.sql.XADataSource JavaDoc;
9 import org.objectweb.jotm.Jotm;
10 import org.objectweb.transaction.jta.TMService;
11
12 import org.enhydra.jdbc.standard.StandardXADataSource;
13
14
15 public class DatabaseHelper {
16
17     private String JavaDoc password;
18     private TMService jotm;
19     private XADataSource JavaDoc xads;
20     private String JavaDoc login;
21     private static final String JavaDoc USER_TRANSACTION_JNDI_NAME = "UserTransaction";
22
23     /**
24      * Constructor for DatabaseHelper.
25      */

26     public DatabaseHelper() {
27         Properties JavaDoc props = new Properties JavaDoc();
28         try {
29             props.load(ClassLoader.getSystemResourceAsStream("spy.properties"));
30         } catch (Exception JavaDoc e) {
31             System.err.println("no properties file found to init the database");
32             System.exit(1);
33         }
34
35         System.out.println("\n database configuration:");
36         props.list(System.out);
37         System.out.println("------------------------\n");
38
39         login = props.getProperty("login");
40         password = props.getProperty("password");
41
42         // Get a transction manager
43
try {
44             // creates an instance of JOTM with a local transaction factory which is not bound to a registry
45
jotm = new Jotm(true, false);
46             InitialContext JavaDoc ictx = new InitialContext JavaDoc();
47             ictx.rebind(USER_TRANSACTION_JNDI_NAME, jotm.getUserTransaction());
48         } catch (Exception JavaDoc e) {
49             e.printStackTrace();
50             System.exit(1);
51         }
52
53         xads = new StandardXADataSource();
54         try {
55             ((StandardXADataSource) xads).setDriverName(props.getProperty("driver"));
56             ((StandardXADataSource) xads).setUrl(props.getProperty("url"));
57             ((StandardXADataSource) xads).setTransactionManager(jotm.getTransactionManager());
58         } catch (Exception JavaDoc e) {
59             e.printStackTrace();
60             System.exit(1);
61         }
62     }
63
64     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
65         XAConnection JavaDoc xaconn = xads.getXAConnection(login, password);
66         return xaconn.getConnection();
67     }
68     /**
69      * Method stop.
70      */

71     public void stop() {
72         try {
73            InitialContext JavaDoc ictx = new InitialContext JavaDoc();
74            ictx.unbind(USER_TRANSACTION_JNDI_NAME);
75         } catch (Exception JavaDoc e) {
76             e.printStackTrace();
77         }
78         jotm.stop();
79         jotm = null;
80     }
81
82 }
83
84
Popular Tags