问题
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:
java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed.*
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:
<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:
<?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
回答1:
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)
.
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.
回答2:
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
Make sure that version is your case at least 5.1.7
来源:https://stackoverflow.com/questions/4648170/why-this-hibernate-mysql-connection-is-read-only