Spring 如果引发异常,则从第一个表回滚数据
我使用SpringMVC和postgres作为数据库 我需要将差异数据插入到两个表中,第二个表有一列作为第一个表的foreignkey 我正在使用jdbc模板连接数据库 如果在插入第二个表时发生异常,我也希望从第一个表回滚数据 为此,我需要使用spring事务概念吗?请建议 我试图以这种方式实现事务Spring 如果引发异常,则从第一个表回滚数据,spring,postgresql-9.1,jdbctemplate,Spring,Postgresql 9.1,Jdbctemplate,我使用SpringMVC和postgres作为数据库 我需要将差异数据插入到两个表中,第二个表有一列作为第一个表的foreignkey 我正在使用jdbc模板连接数据库 如果在插入第二个表时发生异常,我也希望从第一个表回滚数据 为此,我需要使用spring事务概念吗?请建议 我试图以这种方式实现事务 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema
<?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>
有人能提出解决方案吗?你有几种选择。您建议的一个方法是使用Spring事务。 或者,您可以实现自己的ACID方法,在其中共享事务之间的连接使其原子化。看看这个例子。
当您希望完成事务时,密钥是仅在您的连接上提交(“con”)。然后,如果在完成所有微事务之前出现问题,因为您尚未提交任何内容,所以不会在您的数据库中保留任何内容。尝试按照您的想法实施它,如果出现问题,我们将很乐意提供帮助。嘿,JavaBond,我在上面尝试过,但在服务器上遇到异常 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
@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