KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > prevayler > demos > scalability > jdbc > JDBCTransactionConnection


1 //Contributions: Stefan Ortmanns.
2

3 package org.prevayler.demos.scalability.jdbc;
4
5 import org.prevayler.demos.scalability.*;
6 import java.sql.*;
7
8
9 class JDBCTransactionConnection extends JDBCScalabilityConnection implements TransactionConnection {
10
11     private final PreparedStatement updateStatement;
12     private final PreparedStatement deleteStatement;
13
14
15     JDBCTransactionConnection(Connection connection) {
16         super(connection);
17
18         updateStatement = prepare("update " + table() + " set NAME=?,STRING1=?,BIGDECIMAL1=?,BIGDECIMAL2=?,DATE1=?,DATE2=? where ID=?");
19         deleteStatement = prepare("delete from " + table() + " where ID=?");
20     }
21
22
23     protected String JavaDoc table() {
24         return "TRANSACTION_TEST";
25     }
26
27
28     public void performTransaction(Record recordToInsert, Record recordToUpdate, long idToDelete) {
29         insert(recordToInsert);
30         update(recordToUpdate);
31         delete(idToDelete);
32
33         try {
34             connection.commit();
35         } catch (SQLException sqlx) {
36             dealWithSQLException(sqlx, "commiting transaction");
37         }
38     }
39
40
41     private void update(Record recordToUpdate) {
42         try {
43             updateStatement.setString(1,recordToUpdate.getName());
44             updateStatement.setString(2,recordToUpdate.getString1());
45             updateStatement.setBigDecimal(3,recordToUpdate.getBigDecimal1());
46             updateStatement.setBigDecimal(4,recordToUpdate.getBigDecimal2());
47             updateStatement.setDate(5,new java.sql.Date JavaDoc(recordToUpdate.getDate1().getTime()));
48             updateStatement.setDate(6,new java.sql.Date JavaDoc(recordToUpdate.getDate2().getTime()));
49             updateStatement.setLong(7,recordToUpdate.getId()); //"...where ID=?"
50
updateStatement.execute();
51         } catch (SQLException sqlx) {
52             dealWithSQLException(sqlx, "updating record");
53         }
54     }
55
56
57     private void delete(long idToDelete) {
58         try {
59             deleteStatement.setLong(1,idToDelete);
60             deleteStatement.execute();
61         } catch (SQLException sqlx) {
62             dealWithSQLException(sqlx, "deleting record");
63         }
64     }
65 }
Popular Tags