简体   繁体   English

与Hibernate交易代码有关的问题

[英]Question related to Hibernate Transaction code

While I am working on spring boot with hibernate I got some confusion on transaction management code. 在使用休眠模式进行Spring Boot时,我对事务管理代码有些困惑。 Can any one clarify my doubt? 谁能澄清我的疑问?

public class HibernateConfig {

    @Value("${db.driver-class-name}")
    private String DB_DRIVER;
    @Value("${db.password}")
    private String DB_PASSWORD;
    @Value("${db.url}")
    private String DB_URL;
    @Value("${db.username}")
    private String DB_USERNAME;
    @Value("${hibernate.dialect}")
    private String DB_DIALECT;
    @Value("${hibernate.show_sql}")
    private String DB_SHOW_SQL;
    @Value("${entitymanager.packagesToScan}")
    private String DB_PACAKAGESTOSCAN;

    public DataSource dataSource() {
        DriverManagerDataSource datasources = new DriverManagerDataSource();
        datasources.setDriverClassName(DB_DRIVER);
        datasources.setUrl(DB_URL);
        datasources.setUsername(DB_USERNAME);
        datasources.setPassword(DB_PASSWORD);
        return datasources;
    }

    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionfactory = new LocalSessionFactoryBean();
        sessionfactory.setDataSource(dataSource());
        sessionfactory.setPackagesToScan(DB_PACAKAGESTOSCAN);
        Properties hb_properties = new Properties();
        hb_properties.put("hibernate.dialect", DB_DIALECT);
        hb_properties.put("hibernate.show_sql", DB_SHOW_SQL);
        return sessionfactory;

    }

@Bean
public HibernateTransactionManager transactionManager() {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager();
    transactionManager.setSessionFactory(sessionFactory().getObject());
    return transactionManager;

    }

}

Well in the above code the last method transaction manager why we need to write? 在上面的代码中,事务管理器的最后一个方法为什么我们需要编写? In this method what is happening internally can anyone explain me? 用这种方法,内部发生了什么,谁能解释我?

Because hibernate needs transactions in order to perform its operations. 因为休眠需要事务才能执行其操作。 Example would be @OneToMany relationship. 例如@OneToMany关系。 Once an entity is loaded you actualy need an active transaction in order to fetch its Lazy relationships. 实体加载后,实际上需要一个活动事务才能获取其惰性关系。 Only within an active transaction the entity is in managed state. 仅在活动事务中,实体处于托管状态。

Make the following experiment. 做以下实验。 Try to load an entity with Lazy relationships without active transaction, the load will succeed but then try to access one of the relationships. 尝试在没有活动事务的情况下加载具有惰性关系的实体,该加载将成功,但是然后尝试访问这些关系之一。

Without an active transaction you will not be able to perform a simple save operation. 没有活动的事务,您将无法执行简单的保存操作。

Just based on the docs: 仅基于文档:

Binds a Hibernate Session from the specified factory to the thread, potentially allowing for one thread-bound Session per factory. 将一个Hibernate Session从指定的工厂绑定到线程,可能允许每个工厂一个线程绑定的Session。

Basically thanks to a transaction manager you are able to perform various CRUD operations and conform to the ACID database rules. 基本上,借助事务管理器,您可以执行各种CRUD操作并符合ACID数据库规则。

On top of that it: 最重要的是:

Supports custom isolation levels, and timeouts 支持自定义隔离级别和超时

Then if certain operations are only fetching data, then you can set the transaction as readOnly increasing the performance. 然后,如果某些操作仅在获取数据,则可以将事务设置为readOnly提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM