简体   繁体   English

Hibernate对数据库的查询

[英]Hibernate queries on database

I'm having a problem with the amount of queries done by hibernate to the database. 我遇到了hibernate对数据库进行查询的问题。 Here's my query log (using Mysql 5.1) to the database when doing a simple select: 在进行简单的选择时,这是我的查询日志(使用Mysql 5.1)到数据库:

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. 我已经阅读了很多关于将自动提交设置为0然后再设置为1.我知道连接的默认值是1,并且无法更改此行为。 You can run SET autocommit = 0 but the result is the same. 您可以运行SET autocommit = 0但结果是相同的。

Is there anyway to avoid any of those queries? 反正有没有避免任何这些疑问? I don't know why the SELECT @@session.tx_isolation is happening and the rollback. 我不知道为什么SELECT @@ session.tx_isolation正在发生和回滚。 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 我的conf:Spring 2.5.6,Hibernate 3.6.0,Mysql 5.1

datasoure.xml: 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. 但现在的问题是你不能修改(无论我是否设置事务注释readOnly = false)给db给出一个SQLException。 This property sets the connection readonly to true. 此属性将readonly连接设置为true。 I'm guessing there's no way to do this with HibernateTemplate. 我猜测HibernateTemplate无法做到这一点。

I use aspectj transaction for inner code weaving. 我使用aspectj事务进行内码编织。

<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 . 请参阅有关事务管理Spring 2.5.x文档

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. 编辑12/3/11:即使对于仅进行选择的方法,您仍然必须创建只读事务,以便删除您在帖子中提到的额外查询。 Just write @Transactional(readOnly=true) and you should be good to go. 只需编写@Transactional(readOnly = true)就可以了。

Edit 12/20/11: You also need make sure transactions are configured correctly. 编辑12/20/11:您还需要确保正确配置事务。 It looks like the configuration you posted may be missing < tx:annotation-driven /> annotation. 看起来您发布的配置可能缺少<tx:annotation-driven />注释。 See section 10.5.1 of the Spring documentation . 请参阅Spring文档的10.5.1节

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

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