代码之家  ›  专栏  ›  技术社区  ›  Alyona

休眠,如果不使用beginTransaction,会发生什么?

  •  2
  • Alyona  · 技术社区  · 9 年前

    SQLite 图书馆我创建了一个新的 Session session.createNativeQuery("My sql").executeUpdate() 在我关闭我的 一场 一场 Transaction 表演 SQL 交易 ,然后关闭 但并不是所有的例子都有 在他们和我的代码没有它很好地工作。

    • 为什么我们需要使用 交易
    • 如果我们不这样做,会发生什么?
    • 最重要的是,在哪种情况下需要使用它,在哪一种情况下 不

    我使用 <property name="hibernate.connection.pool_size">1</property>

    3 回复  |  直到 9 年前
        1
  •  1
  •   Dhananjay    9 年前

    为什么我们需要使用事务?

    如果我们不这样做,会发生什么?

        2
  •  0
  •   akash777.sharma    9 年前

    您可以尝试将hibernate与JDBC联系起来,您将得到一些关于事务的提示。

    但是,如果您有许多不同的并行任务,它们可能相互依存或独立,该怎么办。然后,您可能需要分别提交/回滚每个任务,或者在任何任务失败时回滚。

    for example
    Big Task :
        small task1
        small task2
        small task3 and many more
    

    在JDBC中,连接接口提供了commit()和rollback()方法。

    在jpa/hibernate中,事务接口提供了commit()和rollback()方法。

    下面是org.hibernate.Transaction的文档

    Allows the application to define units of work, while maintaining 
    abstraction from the underlying transaction implementation (eg. JTA, JDBC). 
    
    A transaction is associated with a Session and is usually 
    initiated by a call to org.hibernate.Session.beginTransaction(). 
    A single session might span multiple transactions since the notion of a session 
    (a conversation between the application and the datastore) is of coarser granularity 
    than the notion of a transaction. However, it is intended that there be at most 
    one uncommitted transaction associated with a particular Session at any time. 
    

    这也可能对你有所帮助 What is the difference between a session and a transaction in JPA 2.0?

        3
  •  0
  •   fg78nc    9 年前

    有两种类型的事务管理或事务划分(将划分视为启动事务、提交或回滚事务):CMT(容器管理事务)-由底层容器为您管理(JTA)和BMT(bean管理事务)-事务划分由开发人员自己编程管理。所以如果你在任何例子中看到 transaction

    当您没有看到显式事务划分时,这意味着这就是CMT。 这是一个非常广泛的话题-我建议你 read more.