1 21 22 package org.opensubsystems.core.persist.db; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import java.sql.Timestamp ; 29 30 import org.opensubsystems.core.error.OSSConcurentModifyException; 31 import org.opensubsystems.core.error.OSSDatabaseAccessException; 32 import org.opensubsystems.core.error.OSSException; 33 import org.opensubsystems.core.error.OSSInternalErrorException; 34 import org.opensubsystems.core.error.OSSInvalidContextException; 35 import org.opensubsystems.core.util.DatabaseUtils; 36 37 46 public final class DatabaseDataUtils 47 { 48 50 53 private DatabaseDataUtils( 54 ) 55 { 56 } 58 59 61 72 public static void checkUpdateError( 73 Connection dbConnection, 74 String strDataName, 75 String strTableName, 76 int iId, 77 Timestamp tmstpModificationDate 78 ) throws OSSException 79 { 80 PreparedStatement psCheck = null; 81 ResultSet rsCheck = null; 82 83 try 84 { 85 StringBuffer sbBuffer = new StringBuffer (); 86 87 sbBuffer.append("select ID, MODIFICATION_DATE from "); 89 sbBuffer.append(strTableName); 90 sbBuffer.append(" where ID = ?"); 91 92 psCheck = dbConnection.prepareStatement(sbBuffer.toString()); 93 psCheck.setInt(1, iId); 94 rsCheck = psCheck.executeQuery(); 95 96 if (rsCheck.next()) 97 { 98 if (rsCheck.getTimestamp("MODIFICATION_DATE").equals(tmstpModificationDate)) 100 { 101 throw new OSSInternalErrorException(strDataName + 104 " has been found within the database but it has not been updated."); 105 } 106 else 107 { 108 throw new OSSConcurentModifyException(strDataName + 111 " has been meanwhile modified by somebody else. Your" + 112 " modifications were not performed to prevent overwriting" + 113 " data you have not seen yet. We suggest you save your changes" + 114 " to a temporary location, reload the data you are trying to" + 115 " modify, review the changes that have been meanwhile done by" + 116 " somebody else and then redo your changes if necessary."); 117 } 118 } 119 else 120 { 121 throw new OSSInvalidContextException( 122 strDataName + " does not exists anymore." + 123 " It might have been meanwhile deleted from the system."); 124 } 125 } 126 catch (SQLException eExc) 127 { 128 throw new OSSDatabaseAccessException( 129 "Unexpected error accessing the database.", eExc); 130 } 131 finally 132 { 133 DatabaseUtils.closeResultSetAndStatement(rsCheck, psCheck); 134 } 135 } 136 } 137
| Popular Tags
|