Nonent1ty 2018-10-22 09:03 采纳率: 0%
浏览 1191

SSH框架的InvalidDataAccessApiUsageException问题

在SSH项目中,执行save方法时报错,求解答。

 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配置文件

<?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: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.xsd
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

         <!-- 配置spring创建容器时要扫描的包 -->
        <context:component-scan base-package="com.wzjpro"></context:component-scan>

        <!-- 配置hibernateTemplate -->
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>

        <!-- 配置sessionFactory -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <!-- 连接数据库,用连接池 -->
            <property name="dataSource" ref="dataSource"></property>
            <!-- hibernate的可选配置 -->
            <property name="hibernateProperties">
                <props>
                    <!-- 数据库的方言 -->
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <!--是否显示hibernate生成的SQL语句 -->
                    <prop key="hibernate.show_sql">true</prop>
                    <!-- 是否使用格式化输出sql语句的控制台 -->
                    <prop key="hibernate.format_sql">false</prop>
                    <!-- 配置hibernate采用何种方式生成DDL语句 -->
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <!-- 把session和线程绑定,从而实现一个线程只有一个Session -->
                    <prop key="hibernate.current_session_context_class">org.springframework.hibernate5.SpringSessionContext</prop>
                </props>
            </property>
            <!-- 指定映射文件的位置 -->
            <property name="packagesToScan">
                <array>
                    <value>com.wzjpro.domain</value>
                </array>
            </property>
        </bean>

        <!-- 配置dataSource -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/crm_ssh"></property>
            <property name="user" value="root"></property>
            <property name="password" value="123456"></property>
        </bean>

        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean> 

        <!-- 开启spring对注解事务的支持 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>

        <!-- 在业务层需要使用事务的地方使用@Transactional注解 -->


</beans>

业务层代码

package com.wzjpro.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wzjpro.dao.ICustomerDao;
import com.wzjpro.domain.Customer;
import com.wzjpro.service.ICustomerService;

/**
 * 客户的业务层实现类
 * @author Administrator
 *
 */
@Service("customerService")
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
public class CustomerServiceImpl implements ICustomerService {

    @Resource(name="customerDao")
    private ICustomerDao customerDao;

    @Override
    public List<Customer> findAllCustomer(DetachedCriteria dCriteria) {
        return customerDao.findAllCustomer(dCriteria);
    }

    @Override
    @Transactional(readOnly=false,propagation=Propagation.REQUIRED)
    public void saveCustomer(Customer customer) {
        customerDao.save(customer);
    }

}
  • 写回答

2条回答

  • 专家-郭老师 Java领域新星创作者 2018-10-22 09:21
    关注
      @Override
        @Transactional(readOnly=false,propagation=Propagation.REQUIRED)
        public void saveCustomer(Customer customer) {
            customerDao.save(customer);
        }
            这里去掉readOnly=false
            如下:
            @Transactional(propagation=Propagation.REQUIRED)
        public void saveCustomer(Customer customer) {
            customerDao.save(customer);
        }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献