1 /* 2 * Copyright 2002-2005 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.ojb.support; 18 19 import javax.sql.DataSource; 20 21 import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; 22 23 /** 24 * Subclass of LocalDataSourceConnectionFactory that returns transaction-aware 25 * proxies for all DataSources retrieved by OJB. 26 * 27 * <p>Define the following entry in your OJB.properties to use this connection factory: 28 * 29 * <pre> 30 * ConnectionFactoryClass=org.springframework.orm.ojb.support.TransactionAwareDataSourceConnectionFactory</pre> 31 * 32 * This connection factory allows Spring's OJB access to participate in JDBC-based 33 * transactions managed outside of OJB (for example, by Spring's DataSourceTransactionManager). 34 * This can be convenient if you need a different local transaction strategy for another O/R 35 * mapping tool, for example, but still want OJB access to join into those transactions. 36 * 37 * <p>A further benefit of this factory is that plain PersistenceBrokers 38 * (opened directly via the PersistenceBrokerFactory, outside of Spring's OJB support) 39 * will still participate in active Spring-managed transactions. 40 * 41 * <p>As a further effect, using a transaction-aware DataSource will apply remaining 42 * transaction timeouts to all created JDBC Statements. This means that all operations 43 * performed by OJB will automatically participate in Spring-managed transaction timeouts. 44 * This is even desirable for transactions managed by PersistenceBrokerTransactionManager. 45 * 46 * @author Juergen Hoeller 47 * @since 1.1.4 48 * @see org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy 49 * @see org.springframework.jdbc.datasource.DataSourceTransactionManager 50 * @see org.springframework.orm.ojb.PersistenceBrokerTransactionManager 51 */ 52 public class TransactionAwareDataSourceConnectionFactory extends LocalDataSourceConnectionFactory { 53 54 /** 55 * Return a TransactionAwareDataSourceProxy for the original DataSource 56 * (i.e. the Spring bean with the JCD alias name), provided that it 57 * isn't a TransactionAwareDataSourceProxy already. 58 */ 59 protected DataSource getDataSource(String jcdAlias) { 60 DataSource originalDataSource = super.getDataSource(jcdAlias); 61 if (originalDataSource instanceof TransactionAwareDataSourceProxy) { 62 return originalDataSource; 63 } 64 else { 65 return new TransactionAwareDataSourceProxy(originalDataSource); 66 } 67 } 68 69 } 70