KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > engine > transaction > hibernate > HibernateDaoTransactionManager


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.hibernate;
17
18 import com.ibatis.common.resources.Resources;
19 import com.ibatis.dao.client.DaoException;
20 import com.ibatis.dao.client.DaoTransaction;
21 import com.ibatis.dao.engine.transaction.DaoTransactionManager;
22 import net.sf.hibernate.SessionFactory;
23 import net.sf.hibernate.cfg.Configuration;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 public class HibernateDaoTransactionManager implements DaoTransactionManager {
29
30   private SessionFactory factory;
31
32   public void configure(Properties JavaDoc properties) {
33     try {
34       Configuration config = new Configuration();
35
36       Iterator JavaDoc it = properties.keySet().iterator();
37       while (it.hasNext()) {
38         String JavaDoc key = (String JavaDoc) it.next();
39         String JavaDoc value = (String JavaDoc) properties.get(key);
40         if (key.startsWith("class.")) {
41           config.addClass(Resources.classForName(value));
42         }
43       }
44
45       Properties JavaDoc props = new Properties JavaDoc();
46       props.putAll(properties);
47       config.setProperties(props);
48
49       factory = config.buildSessionFactory();
50
51     } catch (Exception JavaDoc e) {
52       throw new DaoException("Error configuring Hibernate. Cause: " + e);
53     }
54   }
55
56   public DaoTransaction startTransaction() {
57     return new HibernateDaoTransaction(factory);
58   }
59
60   public void commitTransaction(DaoTransaction trans) {
61     ((HibernateDaoTransaction) trans).commit();
62   }
63
64   public void rollbackTransaction(DaoTransaction trans) {
65     ((HibernateDaoTransaction) trans).rollback();
66   }
67 }
68
Popular Tags