sring事务配置

一、关键配置示例:
[code]
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">












class="org.springframework.transaction.interceptor.TransactionInterceptor"
parent="transactionIntercetorTemplate">

class="org.springframework.orm.hibernate3.HibernateTransactionManager">











PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED,-BussException
PROPAGATION_REQUIRED





class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">







*Service










parent="hibernateDaoTemplate" />
parent="hibernateDaoTemplate" />







[/code]
二、测试代码:
[code]
public void delete(Object entity,IBaseUser user) throws BussException ...{
super.delete(entity, user);
ImageFolder folder = (ImageFolder)entity;
String folderId = folder.getId();
IImageService imageService = ServiceFactory.getImageService();
imageService.deleteAllInFolder(folderId,user);
}
public void deleteAllInFolder(String id,IBaseUser sysUser) throws BussException ...{
log.info("删除图片夹'"+id+"'中的图片...");
throw new BussException(1,"临时测试:测试事务是否生效的异常。测试完毕后删除此语句。",log);
/**//*
StringBuffer sql = new StringBuffer("delete ");
sql.append("from ");
sql.append(getEntityClassName());
SqlBuilder sqlBuilder = SqlBuilder.getInstance();
sqlBuilder.appendWhereCondition(sql, "folderId='"+id+"'");
bulkUpdate(sql.toString(), sysUser);
*/
}
[/code]
三、测试效果:
抛出BussException之后,imageFolder实体并没有被删除,而是回滚了。
四、注意事项:
1.其中的 BussException是一个CheckedException(继承自Exception),这里不过是一个例子,如果其他方法需要同样的方式处理事务,也要加上-BussException,否则不能回滚;如果是一个UnCheckedException,就不需要在这里配置,默认就会回滚。
2.如果采用MySQL,show table status可以查看表的ENGINE类型,MyISAM是不支持事务的,如果需要事务,应该改成InnoDB:alter table [tablename] engine = InnoDB;

你可能感兴趣的:(JAVA)