KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate3 > LocalDataSourceConnectionProvider


1 /*
2  * Copyright 2002-2006 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.orm.hibernate3;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import javax.sql.DataSource JavaDoc;
24
25 import org.hibernate.HibernateException;
26 import org.hibernate.connection.ConnectionProvider;
27 import org.hibernate.util.JDBCExceptionReporter;
28
29 /**
30  * Hibernate connection provider for local DataSource instances
31  * in an application context. This provider will be used if
32  * LocalSessionFactoryBean's "dataSource" property is set.
33  *
34  * @author Juergen Hoeller
35  * @since 1.2
36  * @see LocalSessionFactoryBean#setDataSource
37  */

38 public class LocalDataSourceConnectionProvider implements ConnectionProvider {
39
40     private DataSource JavaDoc dataSource;
41
42     private DataSource JavaDoc dataSourceToUse;
43
44
45     public void configure(Properties JavaDoc props) throws HibernateException {
46         this.dataSource = LocalSessionFactoryBean.getConfigTimeDataSource();
47         // absolutely needs thread-bound DataSource to initialize
48
if (this.dataSource == null) {
49             throw new HibernateException("No local DataSource found for configuration - " +
50                 "dataSource property must be set on LocalSessionFactoryBean");
51         }
52         this.dataSourceToUse = getDataSourceToUse(this.dataSource);
53     }
54
55     /**
56      * Return the DataSource to use for retrieving Connections.
57      * <p>This implementation returns the passed-in DataSource as-is.
58      * @param originalDataSource the DataSource as configured by the user
59      * on LocalSessionFactoryBean
60      * @return the DataSource to actually retrieve Connections from
61      * (potentially wrapped)
62      * @see LocalSessionFactoryBean#setDataSource
63      */

64     protected DataSource JavaDoc getDataSourceToUse(DataSource JavaDoc originalDataSource) {
65         return originalDataSource;
66     }
67
68     /**
69      * Return the DataSource that this ConnectionProvider wraps.
70      */

71     public DataSource JavaDoc getDataSource() {
72         return dataSource;
73     }
74
75     /**
76      * This implementation delegates to the underlying DataSource.
77      * @see javax.sql.DataSource#getConnection()
78      */

79     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
80         try {
81             return this.dataSourceToUse.getConnection();
82         }
83         catch (SQLException JavaDoc ex) {
84             JDBCExceptionReporter.logExceptions(ex);
85             throw ex;
86         }
87     }
88
89     /**
90      * This implementation simply calls <code>Connection.close</code>.
91      * @see java.sql.Connection#close()
92      */

93     public void closeConnection(Connection JavaDoc con) throws SQLException JavaDoc {
94         try {
95             con.close();
96         }
97         catch (SQLException JavaDoc ex) {
98             JDBCExceptionReporter.logExceptions(ex);
99             throw ex;
100         }
101     }
102
103     /**
104      * This implementation does nothing:
105      * We're dealing with an externally managed DataSource.
106      */

107     public void close() {
108     }
109
110     /**
111      * This implementation returns <code>false</code>: We cannot guarantee
112      * to receive the same Connection within a transaction, not even when
113      * dealing with a JNDI DataSource.
114      */

115     public boolean supportsAggressiveRelease() {
116         return false;
117     }
118
119 }
120
Popular Tags