为什么这个 Hibernate MySQL Connection 是只读的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4648170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:16:21  来源:igfitidea点击:

Why this Hibernate MySQL Connection is read-only?

mysqlhibernatespring

提问by El Guapo

I have an application using Spring with Hibernate on a MySQL database. For some reason, as of the last few days, anytime I try to persist any objects to my database I am getting the following error:

我有一个在 MySQL 数据库上使用 Spring 和 Hibernate 的应用程序。出于某种原因,截至最近几天,每当我尝试将任何对象持久保存到我的数据库时,我都会收到以下错误:

java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed.*

java.sql.SQLException:连接是只读的。不允许进行导致数据修改的查询。*

I can't for the life of me figure out why this is happening. My application was working fine a few days ago.

我一生都无法弄清楚为什么会发生这种情况。几天前我的应用程序运行良好。

I am configuring a SessionFactory object in my applicationContext.xml file like this:

我在 applicationContext.xml 文件中配置一个 SessionFactory 对象,如下所示:

     <bean id="sessionFactory" lass="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
         <property name="configLocation"
 value="classpath:/hibernate.cfg.xml"/>
         <property name="packagesToScan">
             <list>
                 <value>com.domain.domainObjects</value>
             </list>
         </property>
     </bean>

my hibernate.cfg.xml file looks like this:

我的 hibernate.cfg.xml 文件如下所示:

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://{url to db}:3306/{db name}</property>
        <property name="connection.username">{db user}</property>
        <property name="connection.password">{db password}</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">10</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- Enable Hibernate's automatic session context management >
        <property name="current_session_context_class">thread</property-->
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

    </session-factory>
</hibernate-configuration>

I am using a the mysql/j conenction version 5.1, hibernate version 3.2, spring mvc 3.0.5

我使用的是 mysql/j 连接版本 5.1、休眠版本 3.2、spring mvc 3.0.5

回答by El Guapo

After about 3 hours of horrible debugging I now know what's going on. I have a service-level method that I also have an "around" advice on. The service-level method is annotated with @Transactional(readOnly=true), however, I have another service in my advice that was annotated with @Transactional(readOnly=false).

经过大约 3 个小时的可怕调试,我现在知道发生了什么。我有一个服务级别的方法,我也有一个“周围”的建议。服务级别方法用 注释@Transactional(readOnly=true),但是,我的建议中有另一个服务是annotated with @Transactional(readOnly=false).

My aspect (or advice) is using the same DAO objects as my normal service-layer, so when I called sessionFactory.getCurrenctSession()it's giving me back the session that was created for my Read-Only Service-level method. Now, I have to re-architect.

我的方面(或建议)使用与我的普通服务层相同的 DAO 对象,所以当我调用sessionFactory.getCurrenctSession()它时,它会返回为我的只读服务级别方法创建的会话。现在,我必须重新架构。

回答by Artem

This looks like the bug they have in MySQL Connector/J for versions up to 5.1.6: http://bugs.mysql.com/bug.php?id=38747

这看起来像他们在 MySQL Connector/J 中的错误,版本高达 5.1.6:http: //bugs.mysql.com/bug.php?id= 38747

Make sure that version is your case at least 5.1.7

确保版本至少是 5.1.7