KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > db > PKManager


1 package org.ashkelon.db;
2 /**
3  * Copyright UptoData Inc. 2001
4  * March 2001
5  */

6
7 import java.sql.Connection JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.ResultSet JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.sql.Statement JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.ashkelon.util.Logger;
18
19 /**
20  * A singleton class that provides function analogous to database "sequences".
21  * It assumes a table has been created named 'seqs' as follows: CREATE TABLE
22  * seqs(seqname VARCHAR2(80) PRIMARY KEY, seqval INTEGER); and that the proper
23  * sequence records and starting values have already been inserted.
24  *
25  * It takes care of returning either currVal or nextVal for any given sequence
26  * name. You must call save() when done to commit back to database.
27  *
28  * Default sequence step level is 1, incrementing.
29  *
30  * @author Eitan Suez
31  */

32 public class PKManager
33 {
34    private static PKManager instance = null;
35    
36    private Logger log;
37    private DBMgr mgr;
38    
39    private Map JavaDoc indices;
40    private int step = 1;
41    private boolean autoCommit = false;
42    
43    //private static String UDPATE_SEQ = "UPDATE SEQS SET SEQVAL=? WHERE SEQNAME=?";
44
//private static String SELECT_SEQS = "SELECT * FROM SEQS";
45
//private static String ADD_SEQ = "INSERT INTO SEQS (SEQNAME, SEQVAL) VALUES (?, ?)";
46
private String JavaDoc UPDATE_SEQ;
47    private String JavaDoc SELECT_SEQS;
48    private String JavaDoc ADD_SEQ;
49    
50    public static PKManager getInstance()
51    {
52       if (instance == null)
53       {
54          instance = new PKManager();
55       }
56       return instance;
57    }
58    
59    private PKManager()
60    {
61       log = Logger.getInstance();
62       
63       mgr = DBMgr.getInstance();
64       
65       Connection JavaDoc conn = null;
66       try
67       {
68          conn = mgr.getConnection();
69          log.debug("connection established to: "+mgr.getConnectionURL());
70
71          UPDATE_SEQ = mgr.getStatement("update_seq");
72          SELECT_SEQS = mgr.getStatement("get_seqs");
73          ADD_SEQ = mgr.getStatement("add_seq");
74          
75          Statement JavaDoc statement = conn.createStatement();
76          ResultSet JavaDoc rs = statement.executeQuery(SELECT_SEQS);
77          indices = new HashMap JavaDoc(20);
78          while (rs.next())
79          {
80             String JavaDoc indexName = rs.getString("SEQNAME");
81             Integer JavaDoc indexValue = new Integer JavaDoc(rs.getInt("SEQVAL"));
82             indices.put(indexName, indexValue);
83          }
84          rs.close();
85          statement.close();
86          mgr.releaseConnection(conn);
87       } catch (SQLException JavaDoc ex)
88       {
89          log.error("Exiting: failed to load indices");
90          DBUtils.logSQLException(ex, SELECT_SEQS);
91          if (conn != null)
92             mgr.releaseConnection(conn);
93          // System.exit(1);
94
}
95    }
96    
97   /**
98    * updates sequences in database. must be called at end of use to maintain
99    * integrity.
100    */

101    public void save()
102    {
103       Connection JavaDoc conn = null;
104       try
105       {
106          conn = mgr.getConnection();
107          //boolean saved = conn.getAutoCommit();
108
conn.setAutoCommit(false);
109          PreparedStatement JavaDoc pstmt = conn.prepareStatement(UPDATE_SEQ);
110          
111          Set JavaDoc set = indices.entrySet();
112          Iterator JavaDoc iter = set.iterator();
113          Map.Entry JavaDoc entry = null;
114          while (iter.hasNext())
115          {
116             entry = (Map.Entry JavaDoc) iter.next();
117             pstmt.clearParameters();
118             pstmt.setInt(1, ((Integer JavaDoc) entry.getValue()).intValue());
119             pstmt.setString(2, (String JavaDoc) entry.getKey());
120             pstmt.executeUpdate();
121          }
122          
123          pstmt.close();
124          conn.commit();
125          //conn.setAutoCommit(saved);
126
} catch (SQLException JavaDoc ex)
127       {
128          log.error("Error occurred attempting to write table index counts");
129          DBUtils.logSQLException(ex, UPDATE_SEQ);
130       }
131       finally
132       {
133          if (conn != null)
134             mgr.releaseConnection(conn);
135       }
136    }
137    
138    private int getIndex(String JavaDoc name)
139    {
140       return ((Integer JavaDoc) indices.get(name)).intValue();
141    }
142    private void setIndex(String JavaDoc name, int index)
143    {
144       indices.put(name, new Integer JavaDoc(index));
145       if (autoCommit)
146       {
147          /*
148          try
149          {
150             PreparedStatement pstmt = conn.prepareStatement(UPDATE_SQL);
151             pstmt.setInt(1, index);
152             pstmt.setString(2, name);
153             pstmt.executeUpdate();
154             pstmt.close();
155             conn.commit();
156          } catch (SQLException ex)
157          {
158             log.tracelnError("Error occurred attempting to commit nextval for sequence "+name);
159             DBUtils.logSQLException(ex);
160          }
161           **/

162       }
163    }
164    
165    /**
166     * get current value for sequence name
167     * @param name sequence name
168     */

169    public int currVal(String JavaDoc name)
170    {
171       return getIndex(name);
172    }
173    
174    /**
175     * get next value for sequence name. default step level is 1 (incrementing).
176     * @param name sequence name
177     */

178    public int nextVal(String JavaDoc name)
179    {
180       int nextVal = currVal(name) + step;
181       setIndex(name, nextVal);
182       return nextVal;
183    }
184    
185    /**
186     * revise step (or increment) value
187     */

188    public void setStep(int step)
189    {
190       this.step = step;
191    }
192    public int getStep()
193    {
194       return step;
195    }
196
197    /*
198    public void setAutoCommit(boolean autoCommit)
199    {
200       this.autoCommit = autoCommit;
201    }
202     */

203    public boolean isAutoCommit()
204    {
205       return autoCommit;
206    }
207    
208    
209    public void addSequence(Connection JavaDoc conn, String JavaDoc seqName, int startvalue) throws SQLException JavaDoc
210    {
211       PreparedStatement JavaDoc pstmt = conn.prepareStatement(ADD_SEQ);
212       pstmt.setString(1, seqName);
213       pstmt.setInt(2, startvalue);
214       pstmt.executeUpdate();
215       pstmt.close();
216    }
217    
218 }
219
Popular Tags