如果抛出异常,则从第一个表回滚数据

时间:2014-12-01 06:25:22

标签: spring postgresql-9.1 jdbctemplate

我使用spring mvc和postgres作为数据库

我要求将diff数据插入到两个表中,第二个表有一列作为第一个表的外键

我正在使用jdbc模板连接数据库

如果在插入第二个表时发生了一些异常,我想从第一个表中回滚数据

为此我需要使用spring transaction概念吗?请建议

我试图以这种方式实现交易

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:security="http://www.springframework.org/schema/security"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


<!-- Define all the service beans that will be created in ePramaan -->
<context:annotation-config />
<tx:annotation-driven/>
<!-- END OF DAO beans Definitions -->

<bean class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource" />

<!-- Create DataSource Bean -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/ePramaanDB" />
</bean>
<bean id="jdbcSPProfileRepository" 
   class="in.cdac.epramaan.sp.dao.JdbcSPProfileRepository">
      <property name="dataSource"  ref="dataSource" />
   </bean>

    <bean id="transactionManager"
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource"  ref="dataSource" />
   </bean>
JdbcSPProfileRepository is a class it contains method to insert data to database
I annotated class with @Transactional
But when i run the server it is throwing exceptions

14:44:01.223 [localhost-startStop-1]错误o.s.web.context.ContextLoader - 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名称为&#39; jdbcSPProfileRepository&#39;:自动连接依赖注入的bean时出错 ncies失败了;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:in.cdac.epramaan.common.bd。 MasterConfigBD in.cdac.epramaan.sp.dao.JdbcSPProfileRepository.masterConfigBD;嵌套异常是org.springframework.beans.factory.NoSuchBeanD efinitionException:找不到类型为[in.cdac.epramaan.common.bd.MasterConfigBD]的限定bean依赖:期望至少有1个bean 有资格作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = t 后悔)}

@Component
@Transactional
public class JdbcSPProfileRepository implements SPProfileRepository {

private static final Logger logger = LoggerFactory
        .getLogger(JdbcSPProfileRepository.class);
@Autowired
private JdbcTemplate jdbcTemplate;

/** The master config bd. */
@Autowired
MasterConfigBD masterConfigBD;

@Autowired
public JdbcSPProfileRepository(DataSource dataSource) {

}

@Override
@Transactional
public SPRegistrationResponse saveSPRegistrationDetails(
        final SPRegistration spreg) {
    SPRegistrationResponse spRegResponse = new SPRegistrationResponse();
    Response response = null;
    logger.debug("In saveSPRegistrationDetails : ");
    try {/////}catch(){}
    }
   }
can anybody please suggest the solution

1 个答案:

答案 0 :(得分:0)

您有几种选择。你建议的一个是使用Spring事务。 http://simplespringtutorial.com/springDeclarativeTransactions.html 或者您可以实现自己的ACID方法,其中共享事务之间的连接使其成为原子。看这个例子。 http://www.tutorialspoint.com/jdbc/commit-rollback.htm

当您想完成交易时,密钥只会提交您的连接(&#34; con&#34;)。然后如果在完成所有微交易之前出现问题,由于您没有提交,因此您的数据库中不会保留任何内容。

相关问题