Revision 8268
Added by Jing Tao about 11 years ago
src/edu/ucsb/nceas/metacat/index/IndexEventDAO.java | ||
---|---|---|
42 | 42 |
public class IndexEventDAO { |
43 | 43 |
|
44 | 44 |
private static IndexEventDAO instance = null; |
45 |
private static String DELETESQL = "delete from index_event where guid = ?"; |
|
45 | 46 |
|
46 | 47 |
private IndexEventDAO() {} |
47 | 48 |
|
... | ... | |
59 | 60 |
try { |
60 | 61 |
// Get a database connection from the pool |
61 | 62 |
dbConn = DBConnectionPool.getDBConnection("IndexEventDAO.add"); |
63 |
dbConn.setAutoCommit(false); |
|
62 | 64 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
63 |
|
|
65 |
//delete the existing event first, because we don't want the table keeps expanding. |
|
66 |
if(event != null && event.getIdentifier() != null && |
|
67 |
event.getIdentifier().getValue() != null && |
|
68 |
!event.getIdentifier().getValue().trim().equals("")) { |
|
69 |
PreparedStatement deleteStmt = dbConn.prepareStatement(DELETESQL); |
|
70 |
deleteStmt.setString(1, event.getIdentifier().getValue()); |
|
71 |
deleteStmt.execute(); |
|
72 |
deleteStmt.close(); |
|
73 |
} |
|
64 | 74 |
// Execute the statement |
65 | 75 |
PreparedStatement stmt = dbConn.prepareStatement(sql); |
66 | 76 |
stmt.setString(1, event.getIdentifier().getValue()); |
... | ... | |
70 | 80 |
|
71 | 81 |
stmt.executeUpdate(); |
72 | 82 |
stmt.close(); |
83 |
dbConn.commit(); |
|
84 |
} catch (SQLException sqle) { |
|
85 |
try { |
|
86 |
if(dbConn != null) { |
|
87 |
//roll back if something happens |
|
88 |
dbConn.rollback(); |
|
89 |
} |
|
90 |
} catch (SQLException sqle2) { |
|
91 |
throw new SQLException("Metacat can't roll back the change since " +sqle2.getMessage(), sqle); |
|
92 |
} |
|
93 |
throw sqle; |
|
73 | 94 |
} finally { |
74 | 95 |
// Return database connection to the pool |
96 |
dbConn.setAutoCommit(true); |
|
75 | 97 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
76 | 98 |
} |
77 | 99 |
} |
78 | 100 |
|
79 | 101 |
public void remove(Identifier identifier) throws SQLException { |
80 |
String sql = "delete from index_event where guid = ?"; |
|
102 |
|
|
81 | 103 |
DBConnection dbConn = null; |
82 | 104 |
int serialNumber = -1; |
83 | 105 |
try { |
... | ... | |
86 | 108 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
87 | 109 |
|
88 | 110 |
// Execute the statement |
89 |
PreparedStatement stmt = dbConn.prepareStatement(sql);
|
|
111 |
PreparedStatement stmt = dbConn.prepareStatement(DELETESQL);
|
|
90 | 112 |
stmt.setString(1, identifier.getValue()); |
91 | 113 |
stmt.execute(); |
92 | 114 |
stmt.close(); |
Also available in: Unified diff
Add code to delete existing records for a id when we try to add the id to the index_event table.