qq_42000615
Nonent1ty
采纳率0%
2018-10-22 09:03

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条回答

  • qq_42000615 Nonent1ty 3年前

    原来是这里写错了

     <prop key="hibernate.current_session_context_class">org.springframework.hibernate5.SpringSessionContext</prop>
    
    
      <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
    
    点赞 评论 复制链接分享
  • qq_23126581 郭老师的小迷弟雅思莫了 3年前
      @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);
        }
    
    
    点赞 评论 复制链接分享

相关推荐