java 只读模式下不允许写入操作 - 持续时出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25222341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:36:55  来源:igfitidea点击:

Write operations are not allowed in read-only mode - Issue while persisting

javaspringhibernatepersistencespring-transactions

提问by Srivatsa N

I have been facing the below error while trying to save the object to database. I tried the solution mentioned here1and here2but no good. I was following a tutorialbut the only difference is versions of Spring and Hibernate.

在尝试将对象保存到数据库时,我遇到了以下错误。我尝试了此处1此处2 中提到的解决方案,但效果不佳。我正在学习教程,但唯一的区别是 Spring 和 Hibernate 的版本。

I am able to persist the object directly using the SessionFactory but it fails with below error if I try this with HibernateDaoSupport

我可以使用 SessionFactory 直接持久化对象,但如果我使用 HibernateDaoSupport 尝试此操作,它会失败并显示以下错误

spring.xml

弹簧文件

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
    <property name="username" value="system" />
    <property name="password" value="xxx" />
</bean>

<context:annotation-config/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="packagesToScan" value="org.sri.sphiber.model"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>



<bean id="customerDAOImpl" class="org.sri.sphiber.dao.CustomerDAOImpl">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

CustomerDAOImpl.java

客户DAOImpl.java

public class CustomerDAOImpl extends HibernateDaoSupport {

    public boolean insertCustomer(Customer cust){

        try {
            getHibernateTemplate().saveOrUpdate(cust);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}

Invoke it using.

调用它使用。

public class MainClass {

    public static void main(String[] args) {


            ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");
            CustomerDAOImpl hdi=appContext.getBean("customerDAOImpl",CustomerDAOImpl.class);


            Customer customer=new Customer();
            customer.setCustomerName("Sri");

            boolean isUpdated = hdi.insertCustomer(customer);

    }


}

Error message.

错误信息。

Aug 10, 2014 12:45:52 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
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.
    at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
    at org.springframework.orm.hibernate4.HibernateTemplate.doInHibernate(HibernateTemplate.java:684)
    at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
    at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
    at org.springframework.orm.hibernate4.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:681)
    at org.sri.sphiber.dao.CustomerDAOImpl.insertCustomer(CustomerDAOImpl.java:16)
    at org.sri.sphiber.main.MainClass.main(MainClass.java:26)

Version Details :

版本详情:

Spring version : spring-framework-4.0.6.RELEASE
Hibernate Version : hibernate-release-4.3.5.Final
Database : Orcale 11g

回答by rgrebski

You are missing TransactionManager definition, see http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/transaction.html

您缺少 TransactionManager 定义,请参阅http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/transaction.html

[UPDATE] Previously i was writing from my mobile so it was hard to provide details, here is what you need to do:

[更新] 以前我是用手机写的,所以很难提供细节,这是你需要做的:

  1. Spring xml config:

    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
    
  2. Add @Transactional annotation to CustomerDaoImpl.insertCustomer method

  1. 弹簧 xml 配置:

    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
    
  2. 给 CustomerDaoImpl.insertCustomer 方法添加@Transactional 注解

Now your code should work.

Please notethat @Transactional annotation should be used in service layer, not in DAO layer like in this example.
@Transactional annotation tells spring to create proxy which "wraps" annotated method with transaction using aspects.

现在你的代码应该可以工作了。

请注意@Transactional 注解应该用在服务层,而不是像这个例子中的 DAO 层。
@Transactional 注释告诉 spring 创建代理,该代理使用方面使用事务“包装”带注释的方法。

回答by SAN

In configuration file

在配置文件中

do the change:-

做改变:-

@Configuration
@EnableTransactionManagement   <-----Put this line
public PersistenceConfig{
//your code
}

(OR)

(或者)

@Bean
@Autowired
public HibernateTemplate getHibernateTemplate(SessionFactory session) {
        HibernateTemplate hb = new HibernateTemplate();
        hb.setCheckWriteOperations(false);
        hb.setSessionFactory(session);
        return hb;
    }