KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > engine > transaction > jta > JtaDaoTransactionManager


1 /*
2  * Copyright 2004 Clinton Begin
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 package com.ibatis.dao.engine.transaction.jta;
17
18 import com.ibatis.dao.client.DaoException;
19 import com.ibatis.dao.client.DaoTransaction;
20 import com.ibatis.dao.engine.transaction.DaoTransactionManager;
21
22 import javax.naming.InitialContext JavaDoc;
23 import javax.sql.DataSource JavaDoc;
24 import javax.transaction.UserTransaction JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 public class JtaDaoTransactionManager implements DaoTransactionManager {
28
29   private DataSource JavaDoc dataSource;
30   private UserTransaction JavaDoc userTransaction;
31
32   public void configure(Properties JavaDoc properties) {
33     String JavaDoc utxName = null;
34     String JavaDoc dsName = null;
35     try {
36       utxName = (String JavaDoc) properties.get("UserTransaction");
37       InitialContext JavaDoc initCtx = new InitialContext JavaDoc();
38       userTransaction = (UserTransaction JavaDoc) initCtx.lookup(utxName);
39       dsName = (String JavaDoc) properties.get("DBJndiContext");
40       dataSource = (DataSource JavaDoc) initCtx.lookup(dsName);
41     } catch (Exception JavaDoc e) {
42       throw new DaoException("Error initializing JTA transaction while looking up UserTransaction (" + utxName + ") or DataSource (" + dsName + "). Cause: " + e);
43     }
44   }
45
46   public DaoTransaction startTransaction() {
47     return new JtaDaoTransaction(userTransaction, dataSource);
48   }
49
50   public void commitTransaction(DaoTransaction trans) {
51     ((JtaDaoTransaction) trans).commit();
52   }
53
54   public void rollbackTransaction(DaoTransaction trans) {
55     ((JtaDaoTransaction) trans).rollback();
56   }
57 }
58
Popular Tags