KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DataBaseLayer


1 /*
2  * @(#) DataBaseLayer.java
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  * This project is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: DataBaseLayer.java,v 1.3 2003/04/11 14:43:34 jmesnil Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 import java.util.Properties JavaDoc;
30
31 import javax.naming.InitialContext JavaDoc;
32 import javax.sql.DataSource JavaDoc;
33 import javax.sql.XADataSource JavaDoc;
34 import javax.transaction.TransactionManager JavaDoc;
35
36 import org.objectweb.jotm.Jotm;
37 import org.objectweb.transaction.jta.TMService;
38
39 import org.enhydra.jdbc.standard.StandardXADataSource;
40 import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
41
42 /**
43  * @author jmesnil
44  */

45 public class DataBaseLayer {
46
47     private static TransactionManager JavaDoc tm;
48
49     public static void main(String JavaDoc[] args) {
50         Properties JavaDoc props = new Properties JavaDoc();
51         try {
52             props.load(ClassLoader.getSystemResourceAsStream("spy.properties"));
53         } catch (Exception JavaDoc e) {
54             System.err.println("no properties file found to init the database");
55             System.exit(1);
56         }
57
58         System.out.println("\n database configuration:");
59         props.list(System.out);
60         System.out.println("------------------------\n");
61
62         try {
63             TMService jotm = new Jotm(true, true);
64             tm = jotm.getTransactionManager();
65
66             StandardXAPoolDataSource ds = null;
67             StandardXADataSource xads = new StandardXADataSource();
68             ds = new StandardXAPoolDataSource(xads);
69             ds.setVerbose(true);
70             ds.setDebug(true);
71
72             xads.setDriverName(props.getProperty("driver"));
73             xads.setUrl(props.getProperty("url"));
74             xads.setUser(props.getProperty("login"));
75             xads.setPassword(props.getProperty("password"));
76             xads.setTransactionManager(tm);
77
78             ds.setUser(props.getProperty("login"));
79             ds.setPassword(props.getProperty("password"));
80             ds.setDataSource(xads);
81             ds.setDataSourceName("XADataSource");
82
83             InitialContext JavaDoc ictx = new InitialContext JavaDoc();
84             ictx.rebind("XADataSource", (XADataSource JavaDoc) xads);
85             System.out.println(
86                 "bound XADataSource with JNDI name 'XADataSource'");
87             //System.out.println("DataSource ref="+ ds.getReference());
88
ictx.rebind("DataSource", (DataSource JavaDoc) ds);
89             System.out.println("bound DataSource with JNDI name 'DataSource'");
90             ictx.rebind("UserTransaction", jotm.getUserTransaction());
91             System.out.println(
92                 "bound UserTransaction with JNDI name 'UserTransaction'");
93             ictx.rebind("javax.transaction.TransactionManager", tm);
94             System.out.println(
95                 "bound TransactionManager JNDI name 'javax.transaction.TransactionManager'");
96
97         } catch (Exception JavaDoc e) {
98             e.printStackTrace();
99         }
100     }
101 }
102
Popular Tags