1 4 package org.oddjob.sql; 5 6 import java.sql.Connection ; 7 import java.sql.PreparedStatement ; 8 import java.sql.ResultSet ; 9 import java.sql.SQLException ; 10 11 import org.oddjob.persist.OddjobPersistException; 12 import org.oddjob.persist.PersisterBase; 13 14 31 public class SqlPersister extends PersisterBase { 32 34 39 private Connection connection; 40 41 private PreparedStatement updateStmt; 42 private PreparedStatement insertStmt; 43 private PreparedStatement selectStmt; 44 private PreparedStatement deleteStmt; 45 46 52 private String prefix = ""; 53 54 59 public void setConnection(Connection connection) throws SQLException { 60 if (this.connection != null) { 61 throw new IllegalStateException ("Connection exists - destroy first."); 62 } 63 this.insertStmt = connection.prepareStatement( 64 "insert into oddjob (prefix, id, job) values (?, ?, ?)"); 65 66 this.updateStmt = connection.prepareStatement( 67 "update oddjob set job = ? where prefix = ? and id = ?"); 68 69 this.selectStmt = connection.prepareStatement( 70 "select job from oddjob where prefix = ? and id = ?"); 71 72 this.deleteStmt = connection.prepareStatement( 73 "delete from oddjob where prefix = ? and id = ?"); 74 75 this.connection = connection; 76 } 77 78 83 public Connection getConnection() { 84 return connection; 85 } 86 87 public void setPrefix(String prefix) { 88 if (prefix == null) { 89 throw new IllegalArgumentException ("Prefix can't be null."); 90 } 91 this.prefix = prefix; 92 } 93 94 public String getPrefix() { 95 return prefix; 96 } 97 98 public void persist(String id, Object o) { 99 try { 100 updateStmt.setObject(1, o); 101 updateStmt.setString(2, prefix); 102 updateStmt.setString(3, id); 103 104 int count = updateStmt.executeUpdate(); 105 if (count == 1) { 106 return; 107 } 108 109 insertStmt.setString(1, prefix); 110 insertStmt.setString(2, id); 111 insertStmt.setObject(3, o); 112 113 insertStmt.execute(); 114 } 115 catch (Exception e) { 116 throw new OddjobPersistException("Failed saving object id [" 117 + id + "], class [" + o.getClass().getName() 118 + "], object [" + o + "]." 119 , e); 120 } 121 } 122 123 public Object restore(String id) { 124 125 try { 126 selectStmt.setString(1, prefix); 127 selectStmt.setString(2, id); 128 129 ResultSet rs = selectStmt.executeQuery(); 130 if (!rs.next()) { 131 return null; 132 } 133 return rs.getObject(1); 134 } 135 catch (Exception e) { 136 throw new OddjobPersistException("Failed to restore object.", e); 137 } 138 } 139 140 public void remove(String id) { 141 try { 142 deleteStmt.setString(1, prefix); 143 deleteStmt.setString(2, id); 144 145 deleteStmt.executeUpdate(); 146 } 147 catch (Exception e) { 148 throw new OddjobPersistException("Failed to restore object.", e); 149 } 150 } 151 152 public void onClose() { 153 Exception ex = null; 154 try { 155 updateStmt.close(); 156 } catch (SQLException e) { 157 ex = e; 158 } 159 try { 160 insertStmt.close(); 161 } catch (SQLException e) { 162 ex = e; 163 } 164 try { 165 selectStmt.close(); 166 } catch (SQLException e) { 167 ex = e; 168 } 169 try { 170 connection.close(); 171 connection = null; 172 } catch (SQLException e) { 173 ex = e; 174 } 175 updateStmt = null; 176 insertStmt = null; 177 selectStmt = null; 178 deleteStmt = null; 179 connection = null; 180 if (ex != null) { 181 throw new RuntimeException (ex); 182 } 183 } 184 } 185 | Popular Tags |