KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > client > template > JdbcDaoTemplate


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.client.template;
17
18 import com.ibatis.dao.client.DaoException;
19 import com.ibatis.dao.client.DaoManager;
20 import com.ibatis.dao.client.DaoTransaction;
21 import com.ibatis.dao.engine.transaction.ConnectionDaoTransaction;
22
23 import java.sql.Connection JavaDoc;
24
25 /**
26  * A DaoTemplate for JDBC implementations that provides a
27  * convenient method to access the JDBC Connection.
28  * <p/>
29  * Use this template for both JDBC and JTA transaction managers.
30  * It can also be used for any transaction manager that supports
31  * normal JDBC connections, including iBATIS SQL Maps and Hibernate.
32  */

33 public abstract class JdbcDaoTemplate extends DaoTemplate {
34
35   /**
36    * The DaoManager that manages this Dao instance will be passed
37    * in as the parameter to this constructor automatically upon
38    * instantiation.
39    *
40    * @param daoManager
41    */

42   public JdbcDaoTemplate(DaoManager daoManager) {
43     super(daoManager);
44   }
45
46   /**
47    * Gets the JDBC Connection associated with the current
48    * DaoTransaction that this Dao is working under.
49    *
50    * @return A JDBC Connection instance.
51    */

52   protected Connection JavaDoc getConnection() {
53     DaoTransaction trans = daoManager.getTransaction(this);
54     if (!(trans instanceof ConnectionDaoTransaction)) {
55       throw new DaoException("The DAO manager of type " + daoManager.getClass().getName() +
56           " cannot supply a JDBC Connection for this template, and is therefore not" +
57           "supported by JdbcDaoTemplate.");
58     }
59     return ((ConnectionDaoTransaction) trans).getConnection();
60   }
61
62 }
63
Popular Tags