在我开始这段似乎很长的段落之前,我想对我可能收到的任何建议表示感谢。-朱约翰
我开发了一个简单的测试来帮助我理解spring的声明性事务管理框架如何与spring的RESTful web服务一起工作。
为此,我开发了以下RESTful控制器:
@RequestMapping(value="register", method=RequestMethod.POST, produces="application/json", consumes="application/json")
public void accountFacade () {
tester.accountTest();
}其中"tester“是以下服务类的bean,并自动进入此控制器。类中的唯一方法是事务性的:
@Transactional
public class TransactionTest extends ActivityStatus {
@Autowired
private ObjectManager objectManager;
@Autowired
private GenericDB gdb;
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/test- context.xml");
TransactionTest tester = (TransactionTest) ac.getBean("tester");
tester.accountTest();
}
@Transactional(readOnly=false, propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED,
rollbackFor=RuntimeException.class)
public void accountTest () {
try {
String sql = "insert into account set account_id=0, ....";
gdb.jdbcTemplateInsert(sql);
sql = "insert into login set account_id=....";
gdb.jdbcTemplateInsert(sql, values);
} catch (DataAccessException dae) {
System.out.println("Catching DataAccessException (dae)");
throw new RuntimeException (dae);
}
}在上面,GenericDB是一个提供自定义jdbcTemplate的类。
测试运行是从浏览器调用accountFacade。我有意设置数据,以便第一个SQL成功,第二个SQL失败,从而触发回滚。
问题:最终的结果是,一个记录被插入考虑,并保持,甚至第二次插入失败的预期。
几个注意事项:
这表明Spring的事务拦截器正确地“包装”了方法"accountTest“。这似乎意味着我的配置(见下文)是有效的。
ApplicationContext.xml中的相关行:
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="tester" class="org.SandRiver.Controllers.TransactionTest">
</bean>关于我的执行环境的几句话:我在OpenShift的环境中运行了这个应用程序,它包括
发布于 2013-09-20 04:39:58
您需要使用InnoDB而不是MyISAM来支持事务。
要检查表的存储引擎:
mysql> show create table your_tbl_name;若要使用InnoDB创建表:
CREATE TABLE your_tbl_name (
col1 VARCHAR(10),
col2 VARCHAR(10)
)
ENGINE = InnoDB;希望这能有所帮助。
https://stackoverflow.com/questions/18750570
复制相似问题