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

El *_*apo 23 mysql spring hibernate

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

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

我不能为我的生活弄清楚为什么会这样.几天前我的申请工作正常.

我正在我的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>
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

我使用的是mysql/j conenction版本5.1,hibernate版本3.2,spring mvc 3.0.5

El *_*apo 47

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

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

  • 我们遇到了完全相同的情况.我们有一个readOnly = true方法,M1,它调用另一个方法M2,即readOnly = false.有趣的是,我们只是在某些时候才看到这个我无法弄清楚的. (2认同)