Spring @Transactional + jdbcTemplate 调用网络服务

Spring @Transactional + jdbcTemplate calling a web service

我的应用程序当前正在使用 Spring 的 JdbcTemplate@Transaction 注释进行事务处理。

我有一个在单个事务上调用 Web 服务 的方法,我设计它以便 Web 服务的异常将回滚事务中的所有数据库更改。

我的问题:如何在调用我的网络服务之前刷新数据库更改?

非常感谢

@Autowired
private MyDao dao;

    @Transactional
    @Override
    public void myMethod() {
        .....
        dao.saveThis(myObjectToSaveIsNotAnIssue);

        // I need to FLUSH here in order for my web service to "see" the saved object

       callWebservice();
    }

我的 spring 配置:

<context:component-scan base-package="com.xxx.xxx" />
    <context:annotation-config />

    <!-- proxy-target-class is set to true to use transactional scope -->
    <tx:annotation-driven proxy-target-class="true" transaction-manager="tomcatTransactionManager" />

    <bean id="sybaseDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/xxx"/>
        <property name="lookupOnStartup" value="true"/>
        <property name="proxyInterface" value="javax.sql.DataSource"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="sybaseDataSource"/>
    </bean>

    <!-- Transaction Manager -->
    <bean id="tomcatTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="sybaseDataSource" />
    </bean>

JDBC 没有任何 "flushing" 概念。 SQL 查询在您执行时执行。内存中不会保留任何要刷新的内容。

由于事务隔离(默认情况下为 READ_COMMITTED),您的 Web 服务不会在数据库中看到您尚未提交的任何内容。如果这确实是您想要的(在 Web 服务中),您必须将隔离级别设置为 READ_UNCOMMITTED:

@Transactional(isolation = Isolation.READ_UNCOMMITTED)

我们可以在汇编程序中从事务中取出 callWebservice() 方法,然后 myMethod 可以首先从汇编程序中调用,然后是 callWebService() 方法。 在调用 Webservice 方法中,我们可以处理成功和失败的场景。 (例如:更新状态)