KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > TransactionTemplate


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

18 package org.apache.activemq.util;
19
20 import java.io.IOException JavaDoc;
21 import org.apache.activemq.broker.ConnectionContext;
22 import org.apache.activemq.store.PersistenceAdapter;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * A helper class for running code with a PersistenceAdapter
28  * in a transaction.
29  *
30  * @version $Revision: 1.2 $
31  */

32 public class TransactionTemplate {
33     static private final Log log=LogFactory.getLog(TransactionTemplate.class);
34     private PersistenceAdapter persistenceAdapter;
35     private ConnectionContext context;
36
37     public TransactionTemplate(PersistenceAdapter persistenceAdapter, ConnectionContext context) {
38         this.persistenceAdapter = persistenceAdapter;
39         this.context=context;
40     }
41
42     public void run(Callback task) throws IOException JavaDoc {
43         persistenceAdapter.beginTransaction(context);
44         Throwable JavaDoc throwable = null;
45         try {
46             task.execute();
47         }
48         catch (IOException JavaDoc t) {
49             throwable = t;
50             throw t;
51         }
52         catch (RuntimeException JavaDoc t) {
53             throwable = t;
54             throw t;
55         }
56         catch (Throwable JavaDoc t) {
57             throwable = t;
58             throw IOExceptionSupport.create("Persistence task failed: "+t, t);
59         } finally {
60             if (throwable == null) {
61                 persistenceAdapter.commitTransaction(context);
62             }
63             else {
64                 log.error("Having to Rollback - caught an exception: " + throwable);
65                 persistenceAdapter.rollbackTransaction(context);
66             }
67         }
68     }
69
70     public ConnectionContext getContext() {
71         return context;
72     }
73
74     public PersistenceAdapter getPersistenceAdapter() {
75         return persistenceAdapter;
76     }
77 }
78
Popular Tags