Domanda

Hi I have a legacy code which gets its jdbc connections through

DataSource.getConnection()

The DataSource is bounded to Jndi namespace.,

Suppose that I have a function which gets its connections like that:

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

And I want to run this foo method into a well defined spring transaction. How would I do that ?

I have used TransactionAwareDataSourceProxy and It worked quite well before I move onto something like JPA

At first I could synronize foo's transaction 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 move onto 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();
    }
}

Now my legacy code blocks do not synchronize with my managed transactions. How can I overcome this problem. Any tools or comments is greatly appreciated.

EDIT

È stato utile?

Soluzione 2

The above configuration is actually correct and the underlying legacy functions is being called inside the created spring transaction. What I miss is that on the upper layer I needed to flush the underlying session to the datastore. When I did that the legacy transaction begins to aware of the changed data and everything works perfect and also the transactions can be rolled back.

I flush my session to the datastore with this :

@Autowired
PlatformTransactionManager pt;

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

Altri suggerimenti

Inject the DataSource and EntityManagerFactory into the JpaTransactionManager bean. See class-level comment @ http://docs.spring.io/spring/docs/3.2.5.RELEASE/javadoc-api/org/springframework/orm/jpa/JpaTransactionManager.html for more info.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top