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

时间:2022-09-11 16:56:20

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 with 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 conenction版本5.1,hibernate版本3.2,spring mvc 3.0.5

2 个解决方案

#1


39  

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)进行注释,但是,我的建议中有另一个使用@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()时,它会返回为我的只读服务级方法创建的会话。现在,我必须重新设计。

#2


0  

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

#1


39  

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)进行注释,但是,我的建议中有另一个使用@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()时,它会返回为我的只读服务级方法创建的会话。现在,我必须重新设计。

#2


0  

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