Spring-JPA 不会导致在只读事务中插入、之后?
事务只读后,持久/插入过程不会执行!
我的applicationContext.xml:
<bean id="transactionManagerConstant" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="constant"/>
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="constant" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="JSFSpring" />
</bean>
Java代码:
@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, readOnly=false)
public String workSaved(){
em.persist(work);
work = (Work) ctx.getBean("work");
return "/list.xhtml";
}
@Transactional(propagation = Propagation.REQUIRED, readOnly=true)
public List<Odev> getWorkedList() {
return workedList = (List<Work>) em.createNamedQuery("Work.findAll").getResultList();
}
异常:javax.persistence.PersistenceException:org.hibernate.exception.GenericJDBCException:无法插入:[com.hakdogan.hakdoganjsfspring.entity.Work]
原因:java .sql.SQLException:连接是只读的。不允许导致数据修改的查询 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) 在 com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2378) 在 com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345) 在 com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330) 在 org.hibernate.id.IdentityGenerator$GetGenerateKeysDelegate.executeAndExtract(IdentityGenerator.java:94) 在 org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) ... 73 更多
The persist/insert process does not executed, after transaction ReadOnly!
My applicationContext.xml:
<bean id="transactionManagerConstant" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="constant"/>
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="constant" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="JSFSpring" />
</bean>
Java code:
@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, readOnly=false)
public String workSaved(){
em.persist(work);
work = (Work) ctx.getBean("work");
return "/list.xhtml";
}
@Transactional(propagation = Propagation.REQUIRED, readOnly=true)
public List<Odev> getWorkedList() {
return workedList = (List<Work>) em.createNamedQuery("Work.findAll").getResultList();
}
Exception: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [com.hakdogan.hakdoganjsfspring.entity.Work]
Caused by: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2378)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 73 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论