KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > support > JdbcDaoSupport


1 /*
2  * Copyright 2002-2007 the original author or authors.
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 org.springframework.jdbc.core.support;
18
19 import java.sql.Connection JavaDoc;
20
21 import javax.sql.DataSource JavaDoc;
22
23 import org.springframework.dao.support.DaoSupport;
24 import org.springframework.jdbc.CannotGetJdbcConnectionException;
25 import org.springframework.jdbc.core.JdbcTemplate;
26 import org.springframework.jdbc.datasource.DataSourceUtils;
27 import org.springframework.jdbc.support.SQLExceptionTranslator;
28
29 /**
30  * Convenient super class for JDBC-based data access objects.
31  *
32  * <p>Requires a {@link javax.sql.DataSource} to be set, providing a
33  * {@link org.springframework.jdbc.core.JdbcTemplate} based on it to
34  * subclasses through the {@link #getJdbcTemplate()} method.
35  *
36  * <p>This base class is mainly intended for JdbcTemplate usage but can
37  * also be used when working with a Connection directly or when using
38  * <code>org.springframework.jdbc.object</code> operation objects.
39  *
40  * @author Juergen Hoeller
41  * @since 28.07.2003
42  * @see #setDataSource
43  * @see #getJdbcTemplate
44  * @see org.springframework.jdbc.core.JdbcTemplate
45  */

46 public abstract class JdbcDaoSupport extends DaoSupport {
47
48     private JdbcTemplate jdbcTemplate;
49
50
51     /**
52      * Set the JDBC DataSource to be used by this DAO.
53      */

54     public final void setDataSource(DataSource JavaDoc dataSource) {
55       this.jdbcTemplate = createJdbcTemplate(dataSource);
56         initTemplateConfig();
57     }
58
59     /**
60      * Create a JdbcTemplate for the given DataSource.
61      * Only invoked if populating the DAO with a DataSource reference!
62      * <p>Can be overridden in subclasses to provide a JdbcTemplate instance
63      * with different configuration, or a custom JdbcTemplate subclass.
64      * @param dataSource the JDBC DataSource to create a JdbcTemplate for
65      * @return the new JdbcTemplate instance
66      * @see #setDataSource
67      */

68     protected JdbcTemplate createJdbcTemplate(DataSource JavaDoc dataSource) {
69         return new JdbcTemplate(dataSource);
70     }
71
72     /**
73      * Return the JDBC DataSource used by this DAO.
74      */

75     public final DataSource JavaDoc getDataSource() {
76         return (this.jdbcTemplate != null ? this.jdbcTemplate.getDataSource() : null);
77     }
78
79     /**
80      * Set the JdbcTemplate for this DAO explicitly,
81      * as an alternative to specifying a DataSource.
82      */

83     public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
84         this.jdbcTemplate = jdbcTemplate;
85         initTemplateConfig();
86     }
87
88     /**
89      * Return the JdbcTemplate for this DAO,
90      * pre-initialized with the DataSource or set explicitly.
91      */

92     public final JdbcTemplate getJdbcTemplate() {
93       return this.jdbcTemplate;
94     }
95
96     /**
97      * Initialize the template-based configuration of this DAO.
98      * Called after a new JdbcTemplate has been set, either directly
99      * or through a DataSource.
100      * <p>This implementation is empty. Subclasses may override this
101      * to configure further objects based on the JdbcTemplate.
102      * @see #getJdbcTemplate()
103      */

104     protected void initTemplateConfig() {
105     }
106
107     protected void checkDaoConfig() {
108         if (this.jdbcTemplate == null) {
109             throw new IllegalArgumentException JavaDoc("'dataSource' or 'jdbcTemplate' is required");
110         }
111     }
112
113
114     /**
115      * Return the SQLExceptionTranslator of this DAO's JdbcTemplate,
116      * for translating SQLExceptions in custom JDBC access code.
117      * @see org.springframework.jdbc.core.JdbcTemplate#getExceptionTranslator()
118      */

119     protected final SQLExceptionTranslator getExceptionTranslator() {
120         return getJdbcTemplate().getExceptionTranslator();
121     }
122
123     /**
124      * Get a JDBC Connection, either from the current transaction or a new one.
125      * @return the JDBC Connection
126      * @throws CannotGetJdbcConnectionException if the attempt to get a Connection failed
127      * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection(javax.sql.DataSource)
128      */

129     protected final Connection JavaDoc getConnection() throws CannotGetJdbcConnectionException {
130         return DataSourceUtils.getConnection(getDataSource());
131     }
132
133     /**
134      * Close the given JDBC Connection, created via this DAO's DataSource,
135      * if it isn't bound to the thread.
136      * @param con Connection to close
137      * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
138      */

139     protected final void releaseConnection(Connection JavaDoc con) {
140         DataSourceUtils.releaseConnection(con, getDataSource());
141     }
142
143 }
144
Popular Tags