1 31 32 package org.opencms.db; 33 34 import org.opencms.file.CmsDataAccessException; 35 import org.opencms.main.CmsException; 36 import org.opencms.main.CmsLog; 37 38 import java.sql.Connection ; 39 import java.sql.DriverManager ; 40 import java.sql.PreparedStatement ; 41 import java.sql.ResultSet ; 42 import java.sql.SQLException ; 43 import java.sql.Timestamp ; 44 import java.util.Hashtable ; 45 46 import org.apache.commons.logging.Log; 47 48 60 public final class CmsDbUtil { 61 62 63 public static final int UNKNOWN_ID = -1; 64 65 66 private static Hashtable c_borderId; 67 68 69 private static Hashtable c_currentId; 70 71 72 private static String c_dbPoolUrl; 73 74 75 private static final int GROW_VALUE = 10; 76 77 78 private static final Log LOG = CmsLog.getLog(CmsDbUtil.class); 79 80 85 private CmsDbUtil() { 86 87 super(); 88 } 89 90 99 public static Timestamp getTimestamp(ResultSet result, String column) throws SQLException { 100 101 int i = 0; 102 for (;;) { 103 try { 104 return (result.getTimestamp(column)); 105 } catch (SQLException exc) { 106 i++; 107 if (i >= 10) { 108 throw exc; 109 } else { 110 if (LOG.isWarnEnabled()) { 111 LOG.warn(Messages.get().getBundle().key(Messages.LOG_GET_TIMESTAMP_2, column, new Integer (i))); 112 } 113 } 114 } 115 } 116 } 117 118 121 public static void init() { 122 123 c_currentId = new Hashtable (); 124 c_borderId = new Hashtable (); 125 c_dbPoolUrl = ""; 126 } 127 128 135 public static int nextId(String tableName) throws CmsException { 136 137 return nextId(c_dbPoolUrl, tableName); 138 } 139 140 148 public static synchronized int nextId(String dbPoolUrl, String tableName) throws CmsDataAccessException { 149 150 String cacheKey = dbPoolUrl + '.' + tableName; 151 152 if (c_currentId.containsKey(cacheKey)) { 154 int id = ((Integer )c_currentId.get(cacheKey)).intValue(); 155 int borderId = ((Integer )c_borderId.get(cacheKey)).intValue(); 156 if (id < borderId) { 157 int nextId = id + 1; 158 c_currentId.put(cacheKey, new Integer (nextId)); 159 return id; 160 } 161 } 162 163 generateNextId(dbPoolUrl, tableName, cacheKey); 167 168 return nextId(dbPoolUrl, tableName); 171 } 172 173 178 public static void setDefaultPool(String dbPoolUrl) { 179 180 c_dbPoolUrl = dbPoolUrl; 181 } 182 183 192 private static void createId(Connection conn, String tableName, int newId) throws CmsDbSqlException { 193 194 PreparedStatement stmt = null; 195 196 try { 197 stmt = conn.prepareStatement("INSERT INTO CMS_SYSTEMID (TABLE_KEY,ID) VALUES (?,?)"); 198 stmt.setString(1, tableName); 199 stmt.setInt(2, newId); 200 stmt.executeUpdate(); 201 } catch (SQLException e) { 202 throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container( 203 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1, 204 CmsDbSqlException.getErrorQuery(stmt)), e); 205 } finally { 206 if (stmt != null) { 207 try { 208 stmt.close(); 209 } catch (SQLException exc) { 210 } 212 } 213 } 214 } 215 216 226 private static void generateNextId(String dbPoolUrl, String tableName, String cacheKey) throws CmsDbSqlException { 227 228 Connection con = null; 229 int id; 230 int borderId; 231 232 try { 233 if (!dbPoolUrl.startsWith(CmsDbPool.DBCP_JDBC_URL_PREFIX)) { 234 dbPoolUrl = CmsDbPool.DBCP_JDBC_URL_PREFIX + dbPoolUrl; 235 } 236 237 con = DriverManager.getConnection(dbPoolUrl); 238 do { 241 id = readId(con, tableName); 242 243 if (id == CmsDbUtil.UNKNOWN_ID) { 244 id = 1; 248 createId(con, tableName, id); 249 } 250 borderId = id + GROW_VALUE; 251 } while (!writeId(con, tableName, id, borderId)); 253 c_currentId.put(cacheKey, new Integer (id)); 255 c_borderId.put(cacheKey, new Integer (borderId)); 256 } catch (SQLException e) { 257 throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container( 258 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_0), e); 259 } finally { 260 if (con != null) { 262 try { 263 con.close(); 264 } catch (SQLException exc) { 265 } 267 } 268 } 269 } 270 271 281 private static int readId(Connection conn, String tableName) throws CmsDbSqlException { 282 283 PreparedStatement stmt = null; 284 ResultSet res = null; 285 try { 286 stmt = conn.prepareStatement("SELECT CMS_SYSTEMID.ID FROM CMS_SYSTEMID WHERE CMS_SYSTEMID.TABLE_KEY=?"); 287 stmt.setString(1, tableName); 288 289 res = stmt.executeQuery(); 290 if (res.next()) { 291 return res.getInt(1); 292 } else { 293 return CmsDbUtil.UNKNOWN_ID; 294 } 295 } catch (SQLException e) { 296 throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container( 297 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1, 298 CmsDbSqlException.getErrorQuery(stmt)), e); 299 } finally { 300 if (res != null) { 302 try { 303 res.close(); 304 } catch (SQLException exc) { 305 } 307 } 308 if (stmt != null) { 309 try { 310 stmt.close(); 311 } catch (SQLException exc) { 312 } 314 } 315 } 316 } 317 318 330 private static boolean writeId(Connection conn, String tableName, int oldId, int newId) throws CmsDbSqlException { 331 332 PreparedStatement stmt = null; 333 334 try { 335 stmt = conn.prepareStatement("UPDATE CMS_SYSTEMID SET ID=? WHERE CMS_SYSTEMID.TABLE_KEY=? AND CMS_SYSTEMID.ID=?"); 336 stmt.setInt(1, newId); 337 stmt.setString(2, tableName); 338 stmt.setInt(3, oldId); 339 int amount = stmt.executeUpdate(); 340 return (amount == 1); 342 } catch (SQLException e) { 343 throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container( 344 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1, 345 CmsDbSqlException.getErrorQuery(stmt)), e); 346 } finally { 347 if (stmt != null) { 348 try { 349 stmt.close(); 350 } catch (SQLException exc) { 351 } 353 } 354 } 355 } 356 } | Popular Tags |