我正在使用spring声明式Transaction功能。像这样的东西
spring configuration
..
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.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.xsd">
<context:annotation-config/>
<context:component-scan base-package="com" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- Add this tag to enable annotations transactions -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
Dao Layer代码
package com.dao;
@Repository("commonDao")
public class CommonDaoImpl implements CommonDao {
private static Logger logger = Logger.getLogger(CommonDaoImpl.class);
@Autowired
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
@Autowired
public void setSessionFactory(){
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public Object makePayment(Object e) {
String sql = "insert into payment (id, name, amount) values('abc', 100)";
try{
return jdbcTemplate.update(sql);
}catch(DataAccessException ex){
throw ex;
}
}
@Override
public Object signUp(Object e) {
String sql = "insert into login (userid, password) values('naveen', 'password')";
return jdbcTemplate.update(sql);
}
}
Service Layer code
@Service
public class CommonServiceImpl implements CommonService {
@Autowired
private CommonDao commonDao;
// @Transactional I tried both of them one by one but not worked
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
@Override
public boolean makePayment() {
try{
commonDao.signUp(new Object());
commonDao.makePayment(new Object());
} catch(DataAccessException ex){
return false;
}
return true;
}
}
但是当我通过控制器发送调用makePayment()
方法然后它将记录保存到登录表但是当它移动到插入到支付表时失败,因为我写了查询以便它可以通过异常。我不明白为什么交易不起作用。因为@Transactional注释是在makePayment方法上所以不应该在db中进行操作。
请告诉我们此代码中的错误。
答案 0 :(得分:0)
SQL
方法中的makePayment
查询是:
insert into payment (id, name, amount) values('abc', 100)
您要求查询在数据库表中插入id
,name
和amount
,但只提供2个值,即abc
和100
。
如果表格中的id
列为autoincrement
个ID,则您无需在SQL
查询中提及该列。 SQL
方法中的makePayment
查询应如下所示:
insert into payment (name, amount) values('abc', 100)