gpt4 book ai didi

java - HibernateTemplate 和 Spring @Transactional 如何协同工作?

转载 作者:行者123 更新时间:2023-11-30 03:59:11 24 4
gpt4 key购买 nike

我们在 DAO 中使用 HibernateTemplate 来执行所有 CRUD 操作。

我的问题是,我们在服务上使用 spring @Transactional ,因为 spring 正在管理事务,所以 HibernateTemplate 在我更新多个的 senario 中表现如何DAO。当使用 Spring @Transactional 时,HibernateTemplate 在不同的 DAO 中使用相同的 session 是什么意思?

@Transactional
public boolean testService(SObject test)[

dao1.save(test.getOne());
dao2.save(test.gettwo());

}


DAO 类如下所示:

public class GenericHibernateDao<T, PK extends Serializable> extends HibernateDaoSupport
.
.
.
public PK save(T newInstance) {
return (PK) getHibernateTemplate().save(newInstance);
}

最佳答案

HibernateTransactionManager javadoc对此非常清楚:

This transaction manager is appropriate for applications that use a single Hibernate SessionFactory for transactional data access, but it also supports direct DataSource access within a transaction (i.e. plain JDBC code working with the same DataSource). This allows for mixing services which access Hibernate and services which use plain JDBC (without being aware of Hibernate)! Application code needs to stick to the same simple Connection lookup pattern as with org.springframework.jdbc.datasource.DataSourceTransactionManager (i.e. DataSourceUtils.getConnection or going through a TransactionAwareDataSourceProxy).

只要您通过知道连接代理的帮助程序类(例如 DataSourceUtils)访问连接(并且 JdbcTemplate 在引擎盖)

关于java - HibernateTemplate 和 Spring @Transactional 如何协同工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22359375/

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