Hibernate对数据库的查询 [英] Hibernate queries on database

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

问题描述

我对hibernate对数据库执行的查询数量有问题。
这是我做查询日志(使用Mysql 5.1)到数据库时做一个简单的选择:

  111125 7:18 :30 
27查询SET autocommit = 0
27查询SELECT @@ session.tx_isolation
27查询选择this_.id为id34_0_,this_.media_id为media3_34_0_,this_.message为message34_0_,this_ .user_id as user4_34_0_ from notifications this_
27查询回滚
27查询SET autocommit = 1

我已经阅读了很多关于将autocommit设置为0,然后设置为1.我知道连接的默认值为1,并且不能更改此行为。你可以运行SET autocommit = 0,但结果是一样的。



有没有反对,以避免任何这些查询?我不知道为什么SELECT @@ session.tx_isolation正在发生和回滚。当我使用一个事务,我得到一个提交,然后回滚。



我的conf:
Spring 2.5.6,Hibernate 3.6.0,Mysql 5.1



datasoure.xml:

 code>< bean id =dataSourcedestroy-method =close
class =org.apache.commons.dbcp.BasicDataSource>
< property name =driverClassNamevalue =$ {jdbc.driverClassName}/>
< property name =urlvalue =$ {jdbc.url}/>
< property name =usernamevalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>

< property name =initialSizevalue =3/>
< property name =maxActivevalue =20/>
< property name =minIdlevalue =3/>
< attribute name =poolPreparedStatementsvalue =false/>
< property name =defaultAutoCommitvalue =false/>
< property name =defaultTransactionIsolationvalue =4/>
< / bean>

交易经理定义:

 < bean id =transactionManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>

UPDATE :通过设置新属性来管理回滚p>

 < property name =defaultReadOnlyvalue =true/> 

但现在的问题是你不能进行修改事务注释readOnly = false)到db给出一个SQLException。
此属性将只读连接设置为true。
我猜想没有办法用HibernateTemplate做这个。



我使用aspectj事务内部编织。

 < aop:aspectj-autoproxy proxy-target-class =true/> 


解决方案

您需要为应用程序配置事务。有关事务管理,请参见 Spring 2.5.x文档。



编辑12/3/11:即使对于只做选择的方法,您仍然必须创建一个只读事务,以便删除您在职位。只需写@Transactional(readOnly = true),你应该很好去。



编辑12/20/11:你还需要确保事务配置正确。您发布的配置可能会丢失< tx:注释驱动的/>注释。请参阅 Spring文档的第10.5.1节


I'm having a problem with the amount of queries done by hibernate to the database. Here's my query log (using Mysql 5.1) to the database when doing a simple select:

111125  7:18:30
27 Query    SET autocommit=0
27 Query    SELECT @@session.tx_isolation
27 Query    select this_.id as id34_0_, this_.media_id as media3_34_0_, this_.message as message34_0_, this_.user_id as user4_34_0_ from notifications this_
27 Query    rollback
27 Query    SET autocommit=1

I've read a lot about setting autocommit to 0 and then to 1. I know that the default for a connection is 1 and this behaviour cannot be changed. You can run SET autocommit = 0 but the result is the same.

Is there anyway to avoid any of those queries? I don't know why the SELECT @@session.tx_isolation is happening and the rollback. When I use a transaction I get a commit and then a rollback. Not sure why a rollback is always cabled.

Thanks a lot!

My conf: Spring 2.5.6, Hibernate 3.6.0, Mysql 5.1

datasoure.xml:

<bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

    <property name="initialSize" value="3" />
    <property name="maxActive" value="20" />
    <property name="minIdle" value="3" />
    <property name="poolPreparedStatements" value="false" />
    <property name="defaultAutoCommit" value="false" />
    <property name="defaultTransactionIsolation" value="4" />
</bean>

Transaction manager definition:

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

UPDATE: Managed to take out the rollback by setting new property

<property name="defaultReadOnly" value="true" />

But the problem now is that you can't make a modification (no matter if I set on the transactional annotation readOnly=false) to the db giving a SQLException. This property sets the connection readonly to true. I'm guessing there's no way to do this with HibernateTemplate.

I use aspectj transaction for inner code weaving.

<aop:aspectj-autoproxy proxy-target-class="true" />

解决方案

You need to configure transactions for your application. See the Spring 2.5.x documentation on transaction management.

Edit 12/3/11: Even for methods that only do selects, you still have to create a read only transaction in order to remove the extra queries you mentioned in your post. Just write @Transactional(readOnly=true) and you should be good to go.

Edit 12/20/11: You also need make sure transactions are configured correctly. It looks like the configuration you posted may be missing < tx:annotation-driven /> annotation. See section 10.5.1 of the Spring documentation.

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

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