1 package org.ashkelon.db; 2 6 7 import java.sql.Connection ; 8 import java.sql.PreparedStatement ; 9 import java.sql.ResultSet ; 10 import java.sql.SQLException ; 11 import java.sql.Statement ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import org.ashkelon.util.Logger; 18 19 32 public class PKManager 33 { 34 private static PKManager instance = null; 35 36 private Logger log; 37 private DBMgr mgr; 38 39 private Map indices; 40 private int step = 1; 41 private boolean autoCommit = false; 42 43 private String UPDATE_SEQ; 47 private String SELECT_SEQS; 48 private String 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 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 statement = conn.createStatement(); 76 ResultSet rs = statement.executeQuery(SELECT_SEQS); 77 indices = new HashMap (20); 78 while (rs.next()) 79 { 80 String indexName = rs.getString("SEQNAME"); 81 Integer indexValue = new Integer (rs.getInt("SEQVAL")); 82 indices.put(indexName, indexValue); 83 } 84 rs.close(); 85 statement.close(); 86 mgr.releaseConnection(conn); 87 } catch (SQLException 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 } 95 } 96 97 101 public void save() 102 { 103 Connection conn = null; 104 try 105 { 106 conn = mgr.getConnection(); 107 conn.setAutoCommit(false); 109 PreparedStatement pstmt = conn.prepareStatement(UPDATE_SEQ); 110 111 Set set = indices.entrySet(); 112 Iterator iter = set.iterator(); 113 Map.Entry entry = null; 114 while (iter.hasNext()) 115 { 116 entry = (Map.Entry ) iter.next(); 117 pstmt.clearParameters(); 118 pstmt.setInt(1, ((Integer ) entry.getValue()).intValue()); 119 pstmt.setString(2, (String ) entry.getKey()); 120 pstmt.executeUpdate(); 121 } 122 123 pstmt.close(); 124 conn.commit(); 125 } catch (SQLException 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 name) 139 { 140 return ((Integer ) indices.get(name)).intValue(); 141 } 142 private void setIndex(String name, int index) 143 { 144 indices.put(name, new Integer (index)); 145 if (autoCommit) 146 { 147 162 } 163 } 164 165 169 public int currVal(String name) 170 { 171 return getIndex(name); 172 } 173 174 178 public int nextVal(String name) 179 { 180 int nextVal = currVal(name) + step; 181 setIndex(name, nextVal); 182 return nextVal; 183 } 184 185 188 public void setStep(int step) 189 { 190 this.step = step; 191 } 192 public int getStep() 193 { 194 return step; 195 } 196 197 203 public boolean isAutoCommit() 204 { 205 return autoCommit; 206 } 207 208 209 public void addSequence(Connection conn, String seqName, int startvalue) throws SQLException 210 { 211 PreparedStatement 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 |