View Javadoc

1   /*
2    *  Copyright 2004 University of Hannover
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy 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,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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              // Selecting the user/group from 
54              // outside the transaction should not work
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              // Selecting the user/group from 
70              // inside the transaction should work
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  }