gpt4 book ai didi

java - 单个 Spring @Transaction 中的两个 dao 方法

转载 作者:行者123 更新时间:2023-12-01 23:34:38 24 4
gpt4 key购买 nike

我正在将 Spring 的 @Transactional 与 Hibernate 一起使用。我试图将两个 dao 方法放入一个事务中,并希望在特定异常时回滚。代码如下:

服务类方法:

@Transactional(propagation=Propagation.REQUIRES_NEW,value="txManager",rollbackFor=TransactionUnSuccessException.class)
public Account makeTransaction(Transaction transaction, String userName)
throws TransactionUnSuccessException {
Account account = null;
account = transferDao.makeTransaction(transaction, userName);
return account;
}

道方法:

@Repository
public class TransferDao extends HibernateDaoSupport {
@Autowired
private SessionFactory sessionFactory;



public Account makeTransaction(Transaction transaction, String userName)
throws TransactionUnSuccessException {
HibernateTemplate hibernateTemplate = getHibernateTemplate();
Account account = null;
Session session = hibernateTemplate.getSessionFactory().openSession();
//session.beginTransaction();
updateSelfAccount(transaction, userName, session);
account = updateAnotherAcccount(transaction, session);
//session.getTransaction().commit();
return account;
}

private void updateSelfAccount(Transaction transaction, String userName,
Session session) {
User currentUser = null;
System.out.println("TransferDao.updateSelfAccount()" + transaction);

Query query = session.createQuery("from User where userName=:userName");
query.setParameter("userName", userName);
currentUser = (User) query.list().get(0);

currentUser.getAccount().getTransactions().add(transaction);
currentUser.getAccount().setAvailableBalance(
currentUser.getAccount().getAvailableBalance()
- transaction.getAmount());
transaction.setTransAccount(currentUser.getAccount());
session.save(transaction);
session.update(currentUser.getAccount());
session.update(currentUser);


private Account updateAnotherAcccount(Transaction transaction,
Session session) throws TransactionUnSuccessException {

Account account = null;
try {
Query query = session
.createQuery("from Account where accNo=:accNo");
query.setParameter("accNo", transaction.getToAcc());
account = (Account) query.list().get(0);
if (account.getAvailableBalance() < 5000) {
account.setAvailableBalance(account.getAvailableBalance()
+ transaction.getAmount());
account.getTransactions().add(transaction);
transaction.setTransAccount(account);
session.save(transaction);
session.update(account);
} else {
throw new TransactionUnSuccessException();
}
} catch (TransactionUnSuccessException te) {
te.printStackTrace();
}

return account;
}
}
}

XML配置:

<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

如果两个方法(updateSelfAccount、updateAnotherAcccount)中的任何一个失败,则整个事务应该回滚。但它无法回滚给定的异常,即使我不确定这一切都发生在单个事务中。请纠正我。

最佳答案

使用 @Transactional 注释的目标是您的代码不应处理事务本身。在您的代码示例中,您使用@Transactional,因此您不必执行类似的事情

session.beginTransaction();

您是否正确设置了 spring 的其他内容

<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="datasource" ref="dataSource"
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

通常sessionFactory是dao中的@Autowired,以便轻松访问您所做的 session

sessionFactory.getCurrentSession()

最后一点,您不需要进行大量的 try catch,然后抛出 TransactionUnSuccessException ,默认情况下,事务将在出现任何异常时回滚。

关于java - 单个 Spring @Transaction 中的两个 dao 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18886209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com