1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package olr.samples;
18
19 import java.sql.Connection;
20
21 import olr.Olr;
22 import olr.om.RdfGroup;
23 import olr.om.RdfGroupPeer;
24 import olr.om.RdfUser;
25 import olr.om.RdfUserPeer;
26
27 import org.apache.torque.Torque;
28 import org.apache.torque.util.Transaction;
29
30 /***
31 * Example showing how to use a transaction using Torque.
32 * The example creates and deletes a group and a user in a transaction.
33 * @version $Id: TransactionSample.java,v 1.4 2004/08/02 18:36:42 roku Exp $
34 */
35 public class TransactionSample
36 {
37 public static void main(String[] args)
38 {
39 Connection connection = null;
40
41 try
42 {
43 Olr.initTorque();
44 connection = Transaction.begin(Torque.getDefaultDB());
45
46 RdfGroup group = new RdfGroup();
47 group.save(connection);
48
49 RdfUser user = new RdfUser();
50 user.setRdfGroup(group);
51 user.save(connection);
52
53
54
55 try
56 {
57 RdfUserPeer.retrieveByPK(user.getPrimaryKey());
58 assert false;
59 }
60 catch(Exception e) {}
61
62 try
63 {
64 RdfGroupPeer.retrieveByPK(group.getPrimaryKey());
65 assert false;
66 }
67 catch(Exception e) {}
68
69
70
71 assert (RdfUserPeer.retrieveByPK(user.getPrimaryKey(), connection) != null);
72 assert (RdfGroupPeer.retrieveByPK(group.getPrimaryKey(), connection) != null);
73
74 RdfUserPeer.doDelete(user, connection);
75 RdfGroupPeer.doDelete(group, connection);
76 Transaction.commit(connection);
77 }
78 catch(Exception e)
79 {
80 try
81 {
82 if(connection != null)
83 Transaction.rollback(connection);
84 }
85 catch(Exception e2) { e.printStackTrace(); }
86 e.printStackTrace();
87 }
88 }
89 }