KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stockonline > ejb > sql > SeqGenerator


1 /*
2  * StockOnline: EJB 1.1 Benchmark.
3  *
4  * Copyright © Commonwealth Scientific and Industrial Research Organisation (CSIRO - www.csiro.au), Australia 2001, 2002, 2003.
5  *
6  * Contact: Paul.Brebner@csiro.au
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or any
11  * later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * Originally developed for the CSIRO Middleware Technology Evaluation (MTE) Project, by
23  * the Software Architectures and Component Technologies Group, CSIRO Mathematical and Information Sciences
24  * Canberra and Sydney, Australia
25  *
26  * www.cmis.csiro.au/sact/
27  * www.cmis.csiro.au/adsat/mte.htm
28  *
29  * Initial developer(s): Shiping Chen, Paul Brebner, Lei Hu, Shuping Ran, Ian Gorton, Anna Liu.
30  * Contributor(s): ______________________.
31  */

32
33
34 //
35
//
36
// History:
37
// 10/08/2001 Shiping Initial coding based on the existing code
38
//
39
// 30/08/2001 Shiping Two modifications:
40
// (1) Encapsulate datasource within the class
41
// (2) Encapsulate the sequence names by providing two
42
// interfaces: getNextAccountID() and getNextTxID().
43
//
44
// 08/11/2001 Shiping Fix the bug: not close the DB connection for
45
// getNextAccountID(String resName) and
46
// getNextTxID(String resName)
47
//
48
// 16/11/2001 Shiping Add interfaces for Datasource
49
//
50
//
51

52 package stockonline.ejb.sql;
53
54 import java.sql.*;
55 import javax.sql.*;
56 import javax.naming.*;
57
58 /** This class implements a sequence generator, which can generate a set
59 * of unique numbers for pre-defined sequence name from the Oracle database.
60 */

61
62 public class SeqGenerator
63 {
64     final static boolean verbose = false;
65     final static String JavaDoc oralceSqlForAccountID = "SELECT SubaccountSeq.nextval FROM dual";
66     final static String JavaDoc oralceSqlForTxID = "SELECT StocktransactionSeq.nextval FROM dual";
67
68         /** Default Contructor
69         */

70     public SeqGenerator () {}
71
72         /** To get next account ID via connection
73     * @param con the connection to the database
74     * @return a sequence number
75         */

76     public static int getNextAccountID(Connection con)
77         throws Exception JavaDoc
78     {
79         return getNext(con, oralceSqlForAccountID);
80     }
81
82         /** To get next account ID via datasource
83     * @param resName datasource
84     * @return a sequence number
85         */

86     public static int getNextAccountID(DataSource ds)
87         throws Exception JavaDoc
88     {
89         Connection con = ds.getConnection();
90         int id = getNext(con, oralceSqlForAccountID);
91         con.close();
92
93         return id;
94     }
95
96         /** To get next account ID vis datasource name
97     * @param resName datasource name
98     * @return a sequence number
99         */

100     public static int getNextAccountID(String JavaDoc resName)
101         throws Exception JavaDoc
102     {
103         Connection con = getConnection(resName);
104         int id = getNext(con, oralceSqlForAccountID);
105         con.close();
106
107         return id;
108     }
109
110     // ----------------------------
111

112         /** To get next tx ID
113     * @param con the connection to the database
114     * @return a sequence number
115         */

116     public static int getNextTxID(Connection con)
117         throws Exception JavaDoc
118     {
119         return getNext(con, oralceSqlForTxID);
120     }
121
122         /** To get next Tx ID via datasource
123     * @param ds datasource
124     * @return a sequence number
125         */

126     public static int getNextTxID(DataSource ds)
127         throws Exception JavaDoc
128     {
129         Connection con = ds.getConnection();
130         int id = getNext(con, oralceSqlForTxID);
131         con.close();
132
133         return id;
134     }
135
136         /** To get next Tx ID
137     * @param resName datasource name
138     * @return a sequence number
139         */

140     public static int getNextTxID(String JavaDoc resName)
141         throws Exception JavaDoc
142     {
143         Connection con = getConnection(resName);
144         int id = getNext(con, oralceSqlForTxID);
145         con.close();
146
147         return id;
148     }
149
150     // ------------------------------
151
// Internal methods
152
// ------------------------------
153

154     private static int getNext(Connection con, String JavaDoc sql)
155         throws Exception JavaDoc
156     {
157         PreparedStatement pstmt = null;
158         ResultSet rs = null;
159         
160         try
161         {
162             if(verbose) System.out.println(sql);
163             pstmt = con.prepareStatement(sql);
164                 rs = pstmt.executeQuery();
165
166                 if (!rs.next())
167                     throw new SQLException("Failed to create the sequence: " + sql);
168
169                 int seqVal = rs.getInt(1);
170             if(verbose) System.out.println("SeqGenerator.getNext(): " + "The generated seqVal = " + seqVal);
171     
172             return seqVal;
173         }
174         catch(SQLException ex)
175         {
176             System.err.println("SeqGenerator.getNext(): " + ex.getMessage());
177             throw new Exception JavaDoc(ex.toString());
178         }
179         finally
180         {
181             if(rs!=null) rs.close();
182             if(pstmt!=null) pstmt.close();
183         }
184     }
185
186         // To get a connection by giving the resource name
187
//
188
private static Connection getConnection (String JavaDoc resName)
189         throws Exception JavaDoc
190     {
191         try
192         {
193             if(verbose) System.out.println("To look up datasource: " + resName);
194             DataSource ds = (DataSource)(new InitialContext()).lookup(resName);
195
196             return ds.getConnection();
197         }
198         catch (javax.naming.NamingException JavaDoc e1)
199         {
200             System.err.println(e1.toString());
201             throw new Exception JavaDoc(e1.toString());
202         }
203         catch(SQLException e2)
204         {
205             System.err.println(e2.toString());
206             throw new Exception JavaDoc(e2.toString());
207         }
208     }
209 }
Popular Tags