How to run legacy code inside spring managed transaction?

Hi, I have legacy code that gets its jdbc connections through

DataSource.getConnection()

DataSource is limited to Jndi. Namespace.,

Suppose I have a function that receives connections like this:

foo(){
   ...
   Connection con = DataSource.getConnection()
   ...
}

And I want to run this foo method in a well-defined spring transaction. How should I do it?

I used TransactionAwareDataSourceProxy and it worked well enough before moving on to something like JPA

At first, I could synchronize transaction foo with my spring transaction with this configuration.

    <tx:annotation-driven transaction-manager="txManager"/>

    <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations"><list><value>classpath:/db.properties</value></list></property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
         <constructor-arg ref="dbcpDataSource"/>
    </bean>

    <bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc:oracle:thin:@${jdbc.url}:1521:${jdbc.db}</value></property>
        <property name="username"><value>${jdbc.username}</value></property>
        <property name="password"><value>${jdbc.password}</value></property>
<!--    <property name="defaultAutoCommit"><value>true</value></property> -->
    </bean>

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

After that, I switch to JPA using LocalContainerEntityManagerFactoryBean and JpaTransactionManager.

package setup;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;


@Configuration
@EnableJpaRepositories
public class SpringContextConfiguration {


    @Bean
    public TestsSetup testSetup(){
        return new TestsSetup();
    }

    @Bean
    public TransactionAwareDataSourceProxy dataSource(){
        TransactionAwareDataSourceProxy tp = new TransactionAwareDataSourceProxy();
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        ds.setUrl("jdbc:oracle:thin:@a.a.a.a:port:some");
        ds.setUsername("user");
        ds.setPassword("paswd");
        ds.setDefaultAutoCommit(true);
        tp.setTargetDataSource(ds);
        return tp;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(dataSource);
        lef.setJpaVendorAdapter(jpaVendorAdapter);
        lef.setPackagesToScan("setup");
        return lef;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(true);
        hibernateJpaVendorAdapter.setGenerateDdl(false);
        hibernateJpaVendorAdapter.setDatabase(Database.ORACLE);
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }
}

. . .

+3
2

spring. , . , , , .

:

@Autowired
PlatformTransactionManager pt;

    TransactionDefinition td = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY);
    pt.getTransaction(td).flush();
0
+1

All Articles