Hibernate只读事务 [英] Hibernate Read-Only Transaction

查看:166
本文介绍了Hibernate只读事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hibernate 文档显示这个例子:

  session = sessionFactory.openSession(); 
session.beginTransaction();
List result = session.createQuery(from Event).list(); (事件事件:(List< Event>)结果){
System.out.println(Event(+ event.getDate()+):+
事件。 getTitle());
}
session.getTransaction()。commit();
session.close();

为什么需要执行 session.getTransaction()。commit )尽管事件列表只是打印出来了吗?

解决方案

SELECT也需要事务处理。没有任何事务执行SELECT是不可能的。在使用某些SQL GUI工具从数据库中选择数据时,您不必显式启动和结束事务,这些工具使用的是自动提交模式。在自动提交模式下,数据库将自动启动并为每个SQL语句提交事务,这样您就不必显式声明事务边界。

如果您不(即提交或回滚)事务时,此事务使用的数据库连接将不会被释放,并且您的数据库最终将耗尽可用连接。

对于hibernate,它默认使用非自动提交模式(由 hibernate.connection.autocommit )。所以我们必须申报交易界限并确保交易结束。



但是,如果您只使用hibernate API查询数据,即使您没有明确声明事务边界,也可以使用以下方法:


  1. 当当前的SQL语句需要一个并且之前没有显式启动事务时,数据库通常会自动启动一个新的事务。

  2. com / javase / 6 / docs / api / java / sql / Connection.html#close%28%29> close()基础 java.sql.Connection#close()被调用并且存在活动事务,此活动事务的结果取决于JDBC供应商的实施。通常,JDBC驱动程序将自动提交或回滚此事务。但在这种情况下,由于您已经获得了您想要的数据,交易是否提交或回滚并不重要。


The Hibernate docs show this example:

session = sessionFactory.openSession();
session.beginTransaction();
List result = session.createQuery( "from Event" ).list();
for ( Event event : (List<Event>) result ) {
    System.out.println( "Event (" + event.getDate() + ") : " + 
        event.getTitle() );
}
session.getTransaction().commit();
session.close();

Why is it necessary to perform the session.getTransaction().commit() even though the Event list was merely printed out?

解决方案

SELECT also requires transaction. It is impossible to execute SELECT without any transaction. The fact that you do not have to explicitly start and end a transaction when selecting data from DB using some SQL GUI tools is that these tools are using autocommit mode . In autocommit mode ,database will automatically start and commit the transaction for each single SQL statement such that you don't have to explicitly declare the transaction boundary.

If you do not end (i.e commit or rollback) a transaction , the database connection used by this transaction will not be released and your database will run out of the available connections eventually.

For hibernate ,it uses non-autocommit mode by default (which is specified by the property hibernate.connection.autocommit) . So we must declare the transaction boundary and make sure the transaction ends.

But if you only query data using hibernate API , it is okay even you do not explicitly declare the transaction boundary because of the followings:

  1. Database will typically start a new transaction automatically when the current SQL statement requires one and no transaction is explicitly started before.

  2. Session.close() will close() the underlying Connection .According to the JDBC specification , if java.sql.Connection#close() is called and there is an active transaction , the result of this active transaction depends on the JDBC vendor 's implementation. Usually , JDBC driver will automatically commit or rollback this transaction . But in this case, it doesn't matter whether the transaction commits or rollbacks because you have already get the data you want.

这篇关于Hibernate只读事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆