开发者社区 问答 正文

spring配置管理hibernateTemplate然后直接注解使用?报错

这是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.


求指导,小弟不是很懂,网上也找了很多教程还是没解决,

展开
收起
爱吃鱼的程序员 2020-06-14 16:14:10 629 分享 版权
阿里云 AI 助理回答

这个错误信息表明你在尝试执行写操作(如保存数据)时,Spring 的事务管理器配置为只读模式(readOnly),这不允许进行任何修改数据库的操作。要解决这个问题,你需要调整事务管理的配置,允许写操作。

在你的Spring配置文件中,问题可能出在事务代理定义的地方,即TransactionProxyFactoryBean的配置部分。你没有提供这部分的具体配置代码,但根据错误提示,很可能是某处将事务标记为了只读。

解决方案

  1. 检查事务代理配置:确保在配置事务代理时没有误将事务设置为只读。例如,如果你有类似于下面的配置,请移除或更正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>
    
  2. 全局事务设置:如果是在全局事务管理器级别设置了只读,比如在<tx:annotation-driven>标签中指定了transaction-manager且该事务管理器被配置为默认只读,那么需要修改相应的配置以允许写操作。

  3. 使用注解控制事务:如果你在服务层使用了@Transactional注解来管理事务,确保对于涉及写操作的方法没有错误地加上readOnly = true。正确的做法是不指定或明确设置为readOnly = false

    @Service
    public class UserService {
       @Autowired
       private DbStorageImpl dbStorage;
    
       @Transactional(readOnly = false) // 确保写操作方法不是只读
       public void createUser(User user) {
           dbStorage.save(user);
       }
    }
    
  4. 检查AOP配置:确认事务相关的切面配置正确无误,没有意外地影响到事务属性的设置。

通过上述步骤应该可以解决你遇到的问题。如果还有其他与阿里云产品相关的问题,或者需要进一步的技术支持,请随时告诉我。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答