KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > sql > SqlPersister


1 /*
2  * Copyright 2004, Rob Gordon.
3  */

4 package org.oddjob.sql;
5
6 import java.sql.Connection JavaDoc;
7 import java.sql.PreparedStatement JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.SQLException JavaDoc;
10
11 import org.oddjob.persist.OddjobPersistException;
12 import org.oddjob.persist.PersisterBase;
13
14 /**
15  * @oddjob.description Persists job state to a database. The database must
16  * have a table which can be created with the following sql.
17  * <pre><code>
18  * CREATE TABLE oddjob(
19  * prefix VARCHAR(20),
20  * id VARCHAR(20),
21  * job OBJECT,
22  * CONSTRAINT oddjob_pk PRIMARY KEY (prefix, id))
23  * </pre></code>
24  *
25  * @oddjob.example
26  *
27  * See {@link org.oddjob.Oddjob}
28  *
29  * @author Rob Gordon.
30  */

31 public class SqlPersister extends PersisterBase {
32 // private static final Logger logger = Logger.getLogger(SqlPersister.class);
33

34     /**
35      * @oddjob.property
36      * @oddjob.description The {@link ConnectionType} to use.
37      * @oddjob.required Yes.
38      */

39     private Connection JavaDoc connection;
40     
41     private PreparedStatement JavaDoc updateStmt;
42     private PreparedStatement JavaDoc insertStmt;
43     private PreparedStatement JavaDoc selectStmt;
44     private PreparedStatement JavaDoc deleteStmt;
45
46     /**
47      * @oddjob.property
48      * @oddjob.description A prefix that can allow the same database to
49      * store many different instances of oddjob.
50      * @oddjob.required No.
51      */

52     private String JavaDoc prefix = "";
53     
54     /**
55      * Set the connection.
56      *
57      * @param connection The connection.
58      */

59     public void setConnection(Connection JavaDoc connection) throws SQLException JavaDoc {
60         if (this.connection != null) {
61             throw new IllegalStateException JavaDoc("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     /**
79      * Getter for connection.
80      *
81      * @return The connection.
82      */

83     public Connection JavaDoc getConnection() {
84         return connection;
85     }
86
87     public void setPrefix(String JavaDoc prefix) {
88         if (prefix == null) {
89             throw new IllegalArgumentException JavaDoc("Prefix can't be null.");
90         }
91         this.prefix = prefix;
92     }
93     
94     public String JavaDoc getPrefix() {
95         return prefix;
96     }
97     
98     public void persist(String JavaDoc id, Object JavaDoc 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 JavaDoc 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 JavaDoc restore(String JavaDoc id) {
124
125         try {
126             selectStmt.setString(1, prefix);
127             selectStmt.setString(2, id);
128
129             ResultSet JavaDoc rs = selectStmt.executeQuery();
130             if (!rs.next()) {
131                 return null;
132             }
133             return rs.getObject(1);
134         }
135         catch (Exception JavaDoc e) {
136             throw new OddjobPersistException("Failed to restore object.", e);
137         }
138     }
139
140     public void remove(String JavaDoc id) {
141         try {
142             deleteStmt.setString(1, prefix);
143             deleteStmt.setString(2, id);
144             
145             deleteStmt.executeUpdate();
146         }
147         catch (Exception JavaDoc e) {
148             throw new OddjobPersistException("Failed to restore object.", e);
149         }
150     }
151     
152     public void onClose() {
153         Exception JavaDoc ex = null;
154         try {
155             updateStmt.close();
156         } catch (SQLException JavaDoc e) {
157             ex = e;
158         }
159         try {
160             insertStmt.close();
161         } catch (SQLException JavaDoc e) {
162             ex = e;
163         }
164         try {
165             selectStmt.close();
166         } catch (SQLException JavaDoc e) {
167             ex = e;
168         }
169         try {
170             connection.close();
171             connection = null;
172         } catch (SQLException JavaDoc 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 JavaDoc(ex);
182         }
183     }
184 }
185
Popular Tags