0
我想使用弹簧交易,并失败。当我设置它们使我的web应用程序将在Tomcat下推出,呼吁春季交易:接口v类
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
当我需要回滚事务,但先前的DB更改不会回滚,我得到了下面的错误我localhost.date.log文件:
SEVERE: Servlet.service() for servlet model threw exception
org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope
at org.springframework.transaction.interceptor.TransactionAspectSupport.currentTransactionStatus(TransactionAspectSupport.java:122)
at edu.mayo.bsi.backslapper.model.dao.impl.TransactServiceDAOImpl.giveProps(TransactServiceDAOImpl.java:160)
我
public interface TransactServiceDAO
和
public class TransactServiceDAOImpl implements TransactServiceDAO
在我的应用上下文-config.xml中
我
<bean id = "transactService" class = "edu.mayo.bsi.backslapper.model.dao.impl.TransactServiceDAOImpl"/>
<!-- enable the configuration of transactional behavior based on annotations -->
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
<tx:advice id = "txAdvice" transaction-manager = "txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name = "get*" read-only = "true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name = "*"/>
</tx:attributes>
</tx:advice>
<!-- ensure that the above transactional advice runs for any execution of an operation defined by the TransactServiceDAO interface -->
<aop:config>
<aop:pointcut id = "transactServiceOperation" expression = "execution(* edu.mayo.bsi.backslapper.model.dao.TransactServiceDAO.*(..))"/>
<aop:advisor advice-ref = "txAdvice" pointcut-ref = "transactServiceOperation"/>
</aop:config>
<bean id = "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name = "dataSource" ref = "backDataSource"/>
</bean>
<tx:advice id = "txAdvice" transaction-manager = "txManager">
<tx:attributes>
<tx:method name = "*" rollback-for = "Throwable"/>
<tx:method name = "*" rollback-for = "java.sql.SQLException"/>
</tx:attributes>
</tx:advice>
下面是一个例子,一个例程,其中一个(计数== 0)回滚不会取消“删除老用户”。
我在做什么错?
public String setUsers (DataSource dataSource, User theUser, String store, AdminUser[] newUsers)
{
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate (dataSource);
Map<String, Object> params = new HashMap<String, Object>();
params.put ("lanID", theUser.getLanID());
params.put ("storeName", store);
int count = namedParameterJdbcTemplate.update (kDeleteOldUsers, params);
for (AdminUser curUser : newUsers)
{
int userID = updateUser (namedParameterJdbcTemplate, curUser.getLanID(), curUser.getFullName(), curUser.getEmail(), null);
if (userID == kError)
userID = addUser (namedParameterJdbcTemplate, curUser);
params.put ("userID", Integer.valueOf (userID));
count = namedParameterJdbcTemplate.update (kAddStoreUser, params);
if (count == 0) // Wasn't inserted
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
params.put ("backslapsToGive", Integer.valueOf (curUser.getBackslapsToGive()));
params.put ("monthlyBackslaps", Integer.valueOf (curUser.getMaxBackslaps()));
count = namedParameterJdbcTemplate.update (kUpdateUserStores, params);
if (count == 0) // Wasn't updated
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
return null;
}
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();抛出异常:重度:在org.springframework.transaction.interceptor.TransactionAspectSupport.currentTransactionStatus(TransactionAspectSupport无交易方面管理的TransactionStatus范围 \t:对servlet模型Servlet.service()抛出异常 org.springframework.transaction.NoTransactionException。 java:122) \t at edu.mayo.bsi.backslapper.model.dao.impl.TransactServiceDAOImpl.giveProps(TransactServiceDAOImpl.java:160) –
很明显,你没有附加tx这个方法。所以首先你需要检查你的aop:切入点是否正确。其次,我不确定这是配置您的TX的最佳方式。这意味着您每次需要添加新服务时都需要更改您的配置。这意味着该服务中的每个方法都是事务性的,并不总是您想要的。我建议你用 设置你的conf,然后使用@Transactional注释方法,并在必要时在实现方法上添加readOnly = true。 –
mommcilo
我的aop:切入点在那里,在中间。它有什么问题吗? TransactService处理所有事务性数据库调用,不管启动服务如何,唯一的事情是事务性的。我试着在例程上使用Transactional,而其他8个类突然无法编译。删除它,并编译。所以我想避免Transactional –