这是spring配置:spring-applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- spring的配置 管理各种bean 包括数据库的配置bean -->
<!-- 配置注解扫描包 -->
<context:component-scan base-package="www.ichint.lostPlatform" />
<!-- 加载文件 -->
<context:property-placeholder location="classpath:spring-config/jdbc.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 1.指定hibernate的属性配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
</props>
<!-- 2.指定hibernate实体类的映射文件 -->
</property>
<property name="configLocations">
<list>
<value>
classpath*:spring-config/hibernate-mapping.xml
</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionBese"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="get*">PROPAGATION_NEVER</prop>
</props>
</property>
</bean>
<!-- Hibernate Template Bean配置 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
下面是:DbStorageImp(dao层代码)
@Repository
public class DbStorageImpl implements DbStorage{
@Autowired
HibernateTemplate jdbcTemplate;
@Override
public void save(User user) {
jdbcTemplate.save(user);
}
}
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
这个错误信息表明你在尝试执行写操作(如保存数据)时,Spring 的事务管理器配置为只读模式(readOnly),这不允许进行任何修改数据库的操作。要解决这个问题,你需要调整事务管理的配置,允许写操作。
在你的Spring配置文件中,问题可能出在事务代理定义的地方,即TransactionProxyFactoryBean
的配置部分。你没有提供这部分的具体配置代码,但根据错误提示,很可能是某处将事务标记为了只读。
检查事务代理配置:确保在配置事务代理时没有误将事务设置为只读。例如,如果你有类似于下面的配置,请移除或更正readOnly
属性:
<property name="transactionAttributes">
<!-- 注意这里 -->
<props>
<!-- 将readOnly="true"改为readOnly="false" 或直接移除readOnly属性 -->
<prop key="save*">PROPAGATION_REQUIRED,-Exception,readOnly</prop>
<!-- 其他方法配置... -->
</props>
</property>
应该更正为:
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
全局事务设置:如果是在全局事务管理器级别设置了只读,比如在<tx:annotation-driven>
标签中指定了transaction-manager
且该事务管理器被配置为默认只读,那么需要修改相应的配置以允许写操作。
使用注解控制事务:如果你在服务层使用了@Transactional注解来管理事务,确保对于涉及写操作的方法没有错误地加上readOnly = true
。正确的做法是不指定或明确设置为readOnly = false
。
@Service
public class UserService {
@Autowired
private DbStorageImpl dbStorage;
@Transactional(readOnly = false) // 确保写操作方法不是只读
public void createUser(User user) {
dbStorage.save(user);
}
}
检查AOP配置:确认事务相关的切面配置正确无误,没有意外地影响到事务属性的设置。
通过上述步骤应该可以解决你遇到的问题。如果还有其他与阿里云产品相关的问题,或者需要进一步的技术支持,请随时告诉我。