KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > impl > jdbcjobstore > oracle > OracleDelegate


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.impl.jdbcjobstore.oracle;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.math.BigDecimal JavaDoc;
28 import java.sql.Blob JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.quartz.Calendar;
36 import org.quartz.CronTrigger;
37 import org.quartz.JobDetail;
38 import org.quartz.SimpleTrigger;
39 import org.quartz.Trigger;
40 import org.quartz.impl.jdbcjobstore.StdJDBCDelegate;
41
42 /**
43  * <p>
44  * This is a driver delegate for the Oracle JDBC driver. To use this delegate,
45  * <code>jdbcDriverVendor</code> should be configured as 'Oracle' with any
46  * <code>jdbcDriverVersion</code>.
47  * </p>
48  *
49  * @see org.quartz.impl.jdbcjobstore.WebLogicDelegate
50  * @see org.quartz.impl.jdbcjobstore.oracle.weblogic.WebLogicOracleDelegate
51  * @author James House
52  * @author Patrick Lightbody
53  * @author Eric Mueller
54  */

55 public class OracleDelegate extends StdJDBCDelegate {
56     /**
57      * <p>
58      * Create new OrcaleDelegate instance.
59      * </p>
60      *
61      * @param logger
62      * the logger to use during execution
63      * @param tablePrefix
64      * the prefix of all table names
65      */

66     public OracleDelegate(Log logger, String JavaDoc tablePrefix, String JavaDoc instanceId) {
67         super(logger, tablePrefix, instanceId);
68     }
69
70     /**
71      * <p>
72      * Create new OrcaleDelegate instance.
73      * </p>
74      *
75      * @param logger
76      * the logger to use during execution
77      * @param tablePrefix
78      * the prefix of all table names
79      * @param useProperties
80      * use java.util.Properties for storage
81      */

82     public OracleDelegate(Log logger, String JavaDoc tablePrefix, String JavaDoc instanceId,
83             Boolean JavaDoc useProperties) {
84         super(logger, tablePrefix, instanceId, useProperties);
85     }
86
87     public static final String JavaDoc UPDATE_ORACLE_JOB_DETAIL = "UPDATE "
88             + TABLE_PREFIX_SUBST + TABLE_JOB_DETAILS + " SET "
89             + COL_DESCRIPTION + " = ?, " + COL_JOB_CLASS + " = ?, "
90             + COL_IS_DURABLE + " = ?, " + COL_IS_VOLATILE + " = ?, "
91             + COL_IS_STATEFUL + " = ?, " + COL_REQUESTS_RECOVERY + " = ? "
92             + " WHERE " + COL_JOB_NAME + " = ? AND " + COL_JOB_GROUP + " = ?";
93
94     public static final String JavaDoc UPDATE_ORACLE_JOB_DETAIL_BLOB = "UPDATE "
95             + TABLE_PREFIX_SUBST + TABLE_JOB_DETAILS + " SET "
96             + COL_JOB_DATAMAP + " = ? " + " WHERE " + COL_JOB_NAME
97             + " = ? AND " + COL_JOB_GROUP + " = ?";
98
99     public static final String JavaDoc UPDATE_ORACLE_JOB_DETAIL_EMPTY_BLOB = "UPDATE "
100             + TABLE_PREFIX_SUBST + TABLE_JOB_DETAILS + " SET "
101             + COL_JOB_DATAMAP + " = EMPTY_BLOB() " + " WHERE " + COL_JOB_NAME
102             + " = ? AND " + COL_JOB_GROUP + " = ?";
103
104     public static final String JavaDoc SELECT_ORACLE_JOB_DETAIL_BLOB = "SELECT "
105             + COL_JOB_DATAMAP + " FROM " + TABLE_PREFIX_SUBST
106             + TABLE_JOB_DETAILS + " WHERE " + COL_JOB_NAME + " = ? AND "
107             + COL_JOB_GROUP + " = ? FOR UPDATE";
108
109     public static final String JavaDoc UPDATE_ORACLE_TRIGGER = "UPDATE "
110         + TABLE_PREFIX_SUBST + TABLE_TRIGGERS + " SET " + COL_JOB_NAME
111         + " = ?, " + COL_JOB_GROUP + " = ?, " + COL_IS_VOLATILE + " = ?, "
112         + COL_DESCRIPTION + " = ?, " + COL_NEXT_FIRE_TIME + " = ?, "
113         + COL_PREV_FIRE_TIME + " = ?, " + COL_TRIGGER_STATE + " = ?, "
114         + COL_TRIGGER_TYPE + " = ?, " + COL_START_TIME + " = ?, "
115         + COL_END_TIME + " = ?, " + COL_CALENDAR_NAME + " = ?, "
116         + COL_MISFIRE_INSTRUCTION + " = ?, "
117         + COL_PRIORITY + " = ? WHERE "
118         + COL_TRIGGER_NAME + " = ? AND " + COL_TRIGGER_GROUP + " = ?";
119
120     
121     public static final String JavaDoc SELECT_ORACLE_TRIGGER_JOB_DETAIL_BLOB = "SELECT "
122         + COL_JOB_DATAMAP + " FROM " + TABLE_PREFIX_SUBST
123         + TABLE_TRIGGERS + " WHERE " + COL_TRIGGER_NAME + " = ? AND "
124         + COL_TRIGGER_GROUP + " = ? FOR UPDATE";
125
126     public static final String JavaDoc UPDATE_ORACLE_TRIGGER_JOB_DETAIL_BLOB = "UPDATE "
127         + TABLE_PREFIX_SUBST + TABLE_TRIGGERS + " SET "
128         + COL_JOB_DATAMAP + " = ? " + " WHERE " + COL_TRIGGER_NAME
129         + " = ? AND " + COL_TRIGGER_GROUP + " = ?";
130
131     public static final String JavaDoc UPDATE_ORACLE_TRIGGER_JOB_DETAIL_EMPTY_BLOB = "UPDATE "
132         + TABLE_PREFIX_SUBST + TABLE_TRIGGERS + " SET "
133         + COL_JOB_DATAMAP + " = EMPTY_BLOB() " + " WHERE " + COL_TRIGGER_NAME
134         + " = ? AND " + COL_TRIGGER_GROUP + " = ?";
135     
136     
137     public static final String JavaDoc INSERT_ORACLE_CALENDAR = "INSERT INTO "
138             + TABLE_PREFIX_SUBST + TABLE_CALENDARS + " (" + COL_CALENDAR_NAME
139             + ", " + COL_CALENDAR + ") " + " VALUES(?, EMPTY_BLOB())";
140
141     public static final String JavaDoc SELECT_ORACLE_CALENDAR_BLOB = "SELECT "
142             + COL_CALENDAR + " FROM " + TABLE_PREFIX_SUBST + TABLE_CALENDARS
143             + " WHERE " + COL_CALENDAR_NAME + " = ? FOR UPDATE";
144
145     public static final String JavaDoc UPDATE_ORACLE_CALENDAR_BLOB = "UPDATE "
146             + TABLE_PREFIX_SUBST + TABLE_CALENDARS + " SET " + COL_CALENDAR
147             + " = ? " + " WHERE " + COL_CALENDAR_NAME + " = ?";
148
149     //---------------------------------------------------------------------------
150
// protected methods that can be overridden by subclasses
151
//---------------------------------------------------------------------------
152

153     protected Object JavaDoc getObjectFromBlob(ResultSet JavaDoc rs, String JavaDoc colName)
154         throws ClassNotFoundException JavaDoc, IOException JavaDoc, SQLException JavaDoc {
155         
156         Object JavaDoc obj = null;
157         InputStream JavaDoc binaryInput = rs.getBinaryStream(colName);
158         if (binaryInput != null) {
159             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(binaryInput);
160             try {
161                 obj = in.readObject();
162             } finally {
163                 in.close();
164             }
165         }
166
167         return obj;
168     }
169
170     public int insertJobDetail(Connection JavaDoc conn, JobDetail job)
171         throws IOException JavaDoc, SQLException JavaDoc {
172
173         ByteArrayOutputStream JavaDoc baos = serializeJobData(job.getJobDataMap());
174         byte[] data = baos.toByteArray();
175         PreparedStatement JavaDoc ps = null;
176         ResultSet JavaDoc rs = null;
177
178         try {
179             ps = conn.prepareStatement(rtp(INSERT_JOB_DETAIL));
180             ps.setString(1, job.getName());
181             ps.setString(2, job.getGroup());
182             ps.setString(3, job.getDescription());
183             ps.setString(4, job.getJobClass().getName());
184             setBoolean(ps, 5, job.isDurable());
185             setBoolean(ps, 6, job.isVolatile());
186             setBoolean(ps, 7, job.isStateful());
187             setBoolean(ps, 8, job.requestsRecovery());
188
189             ps.setBinaryStream(9, null, 0);
190             ps.executeUpdate();
191             ps.close();
192
193             ps = conn
194                     .prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL_EMPTY_BLOB));
195             ps.setString(1, job.getName());
196             ps.setString(2, job.getGroup());
197             ps.executeUpdate();
198             ps.close();
199
200             ps = conn.prepareStatement(rtp(SELECT_ORACLE_JOB_DETAIL_BLOB));
201             ps.setString(1, job.getName());
202             ps.setString(2, job.getGroup());
203
204             rs = ps.executeQuery();
205
206             int res = 0;
207
208             Blob JavaDoc dbBlob = null;
209             if (rs.next()) {
210                 dbBlob = writeDataToBlob(rs, 1, data);
211             } else {
212                 return res;
213             }
214
215             rs.close();
216             ps.close();
217
218             ps = conn.prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL_BLOB));
219             ps.setBlob(1, dbBlob);
220             ps.setString(2, job.getName());
221             ps.setString(3, job.getGroup());
222
223             res = ps.executeUpdate();
224
225             if (res > 0) {
226                 String JavaDoc[] jobListeners = job.getJobListenerNames();
227                 for (int i = 0; jobListeners != null && i < jobListeners.length; i++) {
228                     insertJobListener(conn, job, jobListeners[i]);
229                 }
230             }
231
232             return res;
233         } finally {
234             closeResultSet(rs);
235             closeStatement(ps);
236         }
237
238     }
239
240     protected Object JavaDoc getJobDetailFromBlob(ResultSet JavaDoc rs, String JavaDoc colName)
241         throws ClassNotFoundException JavaDoc, IOException JavaDoc, SQLException JavaDoc {
242         
243         if (canUseProperties()) {
244             InputStream JavaDoc binaryInput = rs.getBinaryStream(colName);
245             return binaryInput;
246         }
247
248         return getObjectFromBlob(rs, colName);
249     }
250
251     public int updateJobDetail(Connection JavaDoc conn, JobDetail job)
252         throws IOException JavaDoc, SQLException JavaDoc {
253         
254         ByteArrayOutputStream JavaDoc baos = serializeJobData(job.getJobDataMap());
255         byte[] data = baos.toByteArray();
256
257         PreparedStatement JavaDoc ps = null;
258         PreparedStatement JavaDoc ps2 = null;
259         ResultSet JavaDoc rs = null;
260
261         try {
262             ps = conn.prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL));
263             ps.setString(1, job.getDescription());
264             ps.setString(2, job.getJobClass().getName());
265             setBoolean(ps, 3, job.isDurable());
266             setBoolean(ps, 4, job.isVolatile());
267             setBoolean(ps, 5, job.isStateful());
268             setBoolean(ps, 6, job.requestsRecovery());
269             ps.setString(7, job.getName());
270             ps.setString(8, job.getGroup());
271
272             ps.executeUpdate();
273             ps.close();
274
275             ps = conn
276                     .prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL_EMPTY_BLOB));
277             ps.setString(1, job.getName());
278             ps.setString(2, job.getGroup());
279             ps.executeUpdate();
280             ps.close();
281
282             ps = conn.prepareStatement(rtp(SELECT_ORACLE_JOB_DETAIL_BLOB));
283             ps.setString(1, job.getName());
284             ps.setString(2, job.getGroup());
285
286             rs = ps.executeQuery();
287
288             int res = 0;
289
290             if (rs.next()) {
291                 Blob JavaDoc dbBlob = writeDataToBlob(rs, 1, data);
292                 ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL_BLOB));
293
294                 ps2.setBlob(1, dbBlob);
295                 ps2.setString(2, job.getName());
296                 ps2.setString(3, job.getGroup());
297
298                 res = ps2.executeUpdate();
299             }
300
301             if (res > 0) {
302                 deleteJobListeners(conn, job.getName(), job.getGroup());
303                 String JavaDoc[] jobListeners = job.getJobListenerNames();
304                 for (int i = 0; jobListeners != null && i < jobListeners.length; i++) {
305                     insertJobListener(conn, job, jobListeners[i]);
306                 }
307             }
308
309             return res;
310
311         } finally {
312             closeResultSet(rs);
313             closeStatement(ps);
314             closeStatement(ps2);
315         }
316     }
317
318     public int insertTrigger(Connection JavaDoc conn, Trigger trigger, String JavaDoc state,
319             JobDetail jobDetail) throws SQLException JavaDoc, IOException JavaDoc {
320
321         byte[] data = null;
322         if (trigger.getJobDataMap().size() > 0) {
323             data = serializeJobData(trigger.getJobDataMap()).toByteArray();
324         }
325         
326         PreparedStatement JavaDoc ps = null;
327         ResultSet JavaDoc rs = null;
328         
329         int insertResult = 0;
330
331         try {
332             ps = conn.prepareStatement(rtp(INSERT_TRIGGER));
333             ps.setString(1, trigger.getName());
334             ps.setString(2, trigger.getGroup());
335             ps.setString(3, trigger.getJobName());
336             ps.setString(4, trigger.getJobGroup());
337             setBoolean(ps, 5, trigger.isVolatile());
338             ps.setString(6, trigger.getDescription());
339             ps.setBigDecimal(7, new BigDecimal JavaDoc(String.valueOf(trigger
340                     .getNextFireTime().getTime())));
341             long prevFireTime = -1;
342             if (trigger.getPreviousFireTime() != null) {
343                 prevFireTime = trigger.getPreviousFireTime().getTime();
344             }
345             ps.setBigDecimal(8, new BigDecimal JavaDoc(String.valueOf(prevFireTime)));
346             ps.setString(9, state);
347             if (trigger.getClass() == SimpleTrigger.class) {
348                 ps.setString(10, TTYPE_SIMPLE);
349             } else if (trigger.getClass() == CronTrigger.class) {
350                 ps.setString(10, TTYPE_CRON);
351             } else {
352                 ps.setString(10, TTYPE_BLOB);
353             }
354             ps.setBigDecimal(11, new BigDecimal JavaDoc(String.valueOf(trigger
355                     .getStartTime().getTime())));
356             long endTime = 0;
357             if (trigger.getEndTime() != null) {
358                 endTime = trigger.getEndTime().getTime();
359             }
360             ps.setBigDecimal(12, new BigDecimal JavaDoc(String.valueOf(endTime)));
361             ps.setString(13, trigger.getCalendarName());
362             ps.setInt(14, trigger.getMisfireInstruction());
363             ps.setBinaryStream(15, null, 0);
364             ps.setInt(16, trigger.getPriority());
365
366             insertResult = ps.executeUpdate();
367
368             if(data != null) {
369                 ps.close();
370
371                 ps = conn
372                     .prepareStatement(rtp(UPDATE_ORACLE_TRIGGER_JOB_DETAIL_EMPTY_BLOB));
373                 ps.setString(1, trigger.getName());
374                 ps.setString(2, trigger.getGroup());
375                 ps.executeUpdate();
376                 ps.close();
377         
378                 ps = conn.prepareStatement(rtp(SELECT_ORACLE_TRIGGER_JOB_DETAIL_BLOB));
379                 ps.setString(1, trigger.getName());
380                 ps.setString(2, trigger.getGroup());
381         
382                 rs = ps.executeQuery();
383         
384                 int res = 0;
385         
386                 Blob JavaDoc dbBlob = null;
387                 if (rs.next()) {
388                     dbBlob = writeDataToBlob(rs, 1, data);
389                 } else {
390                     return res;
391                 }
392         
393                 rs.close();
394                 ps.close();
395         
396                 ps = conn.prepareStatement(rtp(UPDATE_ORACLE_TRIGGER_JOB_DETAIL_BLOB));
397                 ps.setBlob(1, dbBlob);
398                 ps.setString(2, trigger.getName());
399                 ps.setString(3, trigger.getGroup());
400         
401                 res = ps.executeUpdate();
402             }
403             
404         } finally {
405             closeResultSet(rs);
406             closeStatement(ps);
407         }
408
409         if (insertResult > 0) {
410             String JavaDoc[] trigListeners = trigger.getTriggerListenerNames();
411             for (int i = 0; trigListeners != null && i < trigListeners.length; i++) {
412                 insertTriggerListener(conn, trigger, trigListeners[i]);
413             }
414         }
415
416         return insertResult;
417     }
418
419     public int updateTrigger(Connection JavaDoc conn, Trigger trigger, String JavaDoc state,
420             JobDetail jobDetail) throws SQLException JavaDoc, IOException JavaDoc {
421
422         // save some clock cycles by unnecessarily writing job data blob ...
423
boolean updateJobData = trigger.getJobDataMap().isDirty();
424         byte[] data = null;
425         if (updateJobData && trigger.getJobDataMap().size() > 0) {
426             data = serializeJobData(trigger.getJobDataMap()).toByteArray();
427         }
428                 
429         PreparedStatement JavaDoc ps = null;
430         PreparedStatement JavaDoc ps2 = null;
431         ResultSet JavaDoc rs = null;
432         
433         int insertResult = 0;
434
435
436         try {
437             ps = conn.prepareStatement(rtp(UPDATE_ORACLE_TRIGGER));
438                 
439             ps.setString(1, trigger.getJobName());
440             ps.setString(2, trigger.getJobGroup());
441             setBoolean(ps, 3, trigger.isVolatile());
442             ps.setString(4, trigger.getDescription());
443             long nextFireTime = -1;
444             if (trigger.getNextFireTime() != null) {
445                 nextFireTime = trigger.getNextFireTime().getTime();
446             }
447             ps.setBigDecimal(5, new BigDecimal JavaDoc(String.valueOf(nextFireTime)));
448             long prevFireTime = -1;
449             if (trigger.getPreviousFireTime() != null) {
450                 prevFireTime = trigger.getPreviousFireTime().getTime();
451             }
452             ps.setBigDecimal(6, new BigDecimal JavaDoc(String.valueOf(prevFireTime)));
453             ps.setString(7, state);
454             if (trigger.getClass() == SimpleTrigger.class) {
455                 // updateSimpleTrigger(conn, (SimpleTrigger)trigger);
456
ps.setString(8, TTYPE_SIMPLE);
457             } else if (trigger.getClass() == CronTrigger.class) {
458                 // updateCronTrigger(conn, (CronTrigger)trigger);
459
ps.setString(8, TTYPE_CRON);
460             } else {
461                 // updateBlobTrigger(conn, trigger);
462
ps.setString(8, TTYPE_BLOB);
463             }
464             ps.setBigDecimal(9, new BigDecimal JavaDoc(String.valueOf(trigger
465                     .getStartTime().getTime())));
466             long endTime = 0;
467             if (trigger.getEndTime() != null) {
468                 endTime = trigger.getEndTime().getTime();
469             }
470             ps.setBigDecimal(10, new BigDecimal JavaDoc(String.valueOf(endTime)));
471             ps.setString(11, trigger.getCalendarName());
472             ps.setInt(12, trigger.getMisfireInstruction());
473             ps.setInt(13, trigger.getPriority());
474             ps.setString(14, trigger.getName());
475             ps.setString(15, trigger.getGroup());
476
477             insertResult = ps.executeUpdate();
478
479             if(updateJobData) {
480                 ps.close();
481
482                 ps = conn
483                         .prepareStatement(rtp(UPDATE_ORACLE_TRIGGER_JOB_DETAIL_EMPTY_BLOB));
484                 ps.setString(1, trigger.getName());
485                 ps.setString(2, trigger.getGroup());
486                 ps.executeUpdate();
487                 ps.close();
488
489                 ps = conn.prepareStatement(rtp(SELECT_ORACLE_TRIGGER_JOB_DETAIL_BLOB));
490                 ps.setString(1, trigger.getName());
491                 ps.setString(2, trigger.getGroup());
492
493                 rs = ps.executeQuery();
494
495                 int res = 0;
496
497                 if (rs.next()) {
498                     Blob JavaDoc dbBlob = writeDataToBlob(rs, 1, data);
499                     ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_TRIGGER_JOB_DETAIL_BLOB));
500
501                     ps2.setBlob(1, dbBlob);
502                     ps2.setString(2, trigger.getName());
503                     ps2.setString(3, trigger.getGroup());
504
505                     res = ps2.executeUpdate();
506                 }
507             }
508
509         } finally {
510             closeResultSet(rs);
511             closeStatement(ps);
512             closeStatement(ps2);
513         }
514
515         if (insertResult > 0) {
516             deleteTriggerListeners(conn, trigger.getName(), trigger.getGroup());
517
518             String JavaDoc[] trigListeners = trigger.getTriggerListenerNames();
519             for (int i = 0; trigListeners != null && i < trigListeners.length; i++) {
520                 insertTriggerListener(conn, trigger, trigListeners[i]);
521             }
522         }
523
524         return insertResult;
525     }
526     
527     public int insertCalendar(Connection JavaDoc conn, String JavaDoc calendarName,
528             Calendar calendar) throws IOException JavaDoc, SQLException JavaDoc {
529         ByteArrayOutputStream JavaDoc baos = serializeObject(calendar);
530
531         PreparedStatement JavaDoc ps = null;
532         PreparedStatement JavaDoc ps2 = null;
533         ResultSet JavaDoc rs = null;
534
535         try {
536             ps = conn.prepareStatement(rtp(INSERT_ORACLE_CALENDAR));
537             ps.setString(1, calendarName);
538
539             ps.executeUpdate();
540             ps.close();
541
542             ps = conn.prepareStatement(rtp(SELECT_ORACLE_CALENDAR_BLOB));
543             ps.setString(1, calendarName);
544
545             rs = ps.executeQuery();
546
547             if (rs.next()) {
548                 Blob JavaDoc dbBlob = writeDataToBlob(rs, 1, baos.toByteArray());
549                 ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_CALENDAR_BLOB));
550
551                 ps2.setBlob(1, dbBlob);
552                 ps2.setString(2, calendarName);
553
554                 return ps2.executeUpdate();
555             }
556
557             return 0;
558
559         } finally {
560             closeResultSet(rs);
561             closeStatement(ps);
562             closeStatement(ps2);
563         }
564     }
565
566     public int updateCalendar(Connection JavaDoc conn, String JavaDoc calendarName,
567             Calendar calendar) throws IOException JavaDoc, SQLException JavaDoc {
568         ByteArrayOutputStream JavaDoc baos = serializeObject(calendar);
569
570         PreparedStatement JavaDoc ps = null;
571         PreparedStatement JavaDoc ps2 = null;
572         ResultSet JavaDoc rs = null;
573
574         try {
575             ps = conn.prepareStatement(rtp(SELECT_ORACLE_CALENDAR_BLOB));
576             ps.setString(1, calendarName);
577
578             rs = ps.executeQuery();
579
580             if (rs.next()) {
581                 Blob JavaDoc dbBlob = writeDataToBlob(rs, 1, baos.toByteArray());
582                 ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_CALENDAR_BLOB));
583
584                 ps2.setBlob(1, dbBlob);
585                 ps2.setString(2, calendarName);
586
587                 return ps2.executeUpdate();
588             }
589
590             return 0;
591
592         } finally {
593             closeResultSet(rs);
594             closeStatement(ps);
595             closeStatement(ps2);
596         }
597     }
598
599     public int updateJobData(Connection JavaDoc conn, JobDetail job)
600         throws IOException JavaDoc, SQLException JavaDoc {
601         
602         ByteArrayOutputStream JavaDoc baos = serializeJobData(job.getJobDataMap());
603         byte[] data = baos.toByteArray();
604
605         PreparedStatement JavaDoc ps = null;
606         PreparedStatement JavaDoc ps2 = null;
607         ResultSet JavaDoc rs = null;
608
609         try {
610             ps = conn.prepareStatement(rtp(SELECT_ORACLE_JOB_DETAIL_BLOB));
611             ps.setString(1, job.getName());
612             ps.setString(2, job.getGroup());
613
614             rs = ps.executeQuery();
615
616             int res = 0;
617
618             if (rs.next()) {
619                 Blob JavaDoc dbBlob = writeDataToBlob(rs, 1, data);
620                 ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL_BLOB));
621
622                 ps2.setBlob(1, dbBlob);
623                 ps2.setString(2, job.getName());
624                 ps2.setString(3, job.getGroup());
625
626                 res = ps2.executeUpdate();
627             }
628
629             return res;
630         } finally {
631             closeResultSet(rs);
632             closeStatement(ps);
633             closeStatement(ps2);
634         }
635     }
636
637     protected Blob JavaDoc writeDataToBlob(ResultSet JavaDoc rs, int column, byte[] data) throws SQLException JavaDoc {
638
639         Blob JavaDoc blob = rs.getBlob(column); // get blob
640

641         if (blob == null) {
642             throw new SQLException JavaDoc("Driver's Blob representation is null!");
643         }
644         
645         if (blob instanceof oracle.sql.BLOB) { // is it an oracle blob?
646
((oracle.sql.BLOB) blob).putBytes(1, data);
647             return blob;
648         } else {
649             throw new SQLException JavaDoc(
650                     "Driver's Blob representation is of an unsupported type: "
651                             + blob.getClass().getName());
652         }
653     }
654 }
655
656 // EOF
657
Popular Tags