我正在研究动态Web应用程序,我陷入了这个错误:
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.
StackOverFlow中提出的解决方案并没有解决我的问题!我无法找到我使用只读模式的位置!
当我想使用我的jsf页面在DataBase中保存新的干预时出现这个错误。我正在使用Netbeans的默认连接器DataBase,使用Glassfish4,spring和hibernate 4.这是堆栈跟踪:
Caused by: 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$12.doInHibernate(HibernateTemplate.java:620)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)
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.save(HibernateTemplate.java:617)
at dao.ImplIntervention.insert(ImplIntervention.java:29)
at bo.InterventionImplBO.insert(InterventionImplBO.java:47)
at jsf.bean.BeanIntervention.insert(BeanIntervention.java:123)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at javax.el.ELUtil.invokeMethod(ELUtil.java:332)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:537)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
at com.sun.el.parser.AstValue.invoke(AstValue.java:283)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 36 more
ImplIntervention.java:
public class ImplIntervention extends HibernateDaoSupport implements IfaceIntervention{
@Autowired
public SessionFactory sessionFactory;
@Override
@Transactional
public void insert(Intervention obj) {
getHibernateTemplate().save(obj);
}
@Override
public void delete(Intervention obj) {
getHibernateTemplate().delete(obj);
}
@Override
public void update(Intervention obj) {
getHibernateTemplate().merge(obj);
}
@Override
public List<Intervention> getAll() {
return (List<Intervention>) (Object) getHibernateTemplate().find("from intervention");
}
}
InterventionImplBO.java:
public class InterventionImplBO {
private ImplIntervention interventionDAO;
public ImplIntervention getInterventionDAO() {
return interventionDAO;
}
public void setInterventionDAO(ImplIntervention interventionDAO) {
this.interventionDAO = interventionDAO;
}
public void insert(BeanIntervention obj) {
Intervention intervention = new Intervention();
intervention.setCodem(obj.getCodem());
intervention.setCodereparation(obj.getCodereparation());
intervention.setDatedemandei(obj.getDatei());
intervention.setDatei(obj.getDatei());
intervention.setDescriptioni(obj.getDescriptioni());
intervention.setLogin(obj.getLogin());
intervention.setNumi(obj.getNumi());
intervention.setObjeti(obj.getObjeti());
interventionDAO.insert(intervention);
}
public void delete(BeanIntervention obj) {
Intervention intervention = new Intervention();
intervention.setNumi(obj.getNumi());
interventionDAO.delete(intervention);
}
public void update(BeanIntervention obj) {
Intervention intervention = new Intervention();
intervention.setCodem(obj.getCodem());
intervention.setCodereparation(obj.getCodereparation());
intervention.setDatedemandei(obj.getDatei());
intervention.setDatei(obj.getDatei());
intervention.setDescriptioni(obj.getDescriptioni());
intervention.setLogin(obj.getLogin());
intervention.setNumi(obj.getNumi());
intervention.setObjeti(obj.getObjeti());
interventionDAO.update(intervention);
}
public List<BeanIntervention> getAll() {
List<BeanIntervention> liste = new ArrayList<BeanIntervention>();
for (Intervention obj: interventionDAO.getAll())
{
BeanIntervention intervention = new BeanIntervention();
intervention.setCodem(obj.getCodem());
intervention.setCodereparation(obj.getCodereparation());
intervention.setDatedemandei(obj.getDatei());
intervention.setDatei(obj.getDatei());
intervention.setDescriptioni(obj.getDescriptioni());
intervention.setLogin(obj.getLogin());
intervention.setNumi(obj.getNumi());
intervention.setObjeti(obj.getObjeti());
}
return liste;
}
}
applicationContext.xml:
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"></property>
<property name="url" value="jdbc:derby://localhost:1527/InterventionsDB;create=true"></property>
<property name="username" value="****"/>
<property name="password" value="****"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref = "dataSource"/>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="connection.autoReconnect">true</prop>
<prop key="connection.autoReconnectForPools">true</prop>
<prop key="connection.is-connection-validation-required">true</prop>
</props>
</property>
</bean>
<bean id ="interventionDAO" class = "dao.ImplIntervention">
<property name ="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id ="interventionBO" class = "bo.InterventionImplBO">
<property name ="interventionDAO" ref="interventionDAO"></property>
</bean>
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
答案 0 :(得分:0)
您需要在应用程序上下文中配置PlatformTransactionManager以处理@Transactional注释。修改应用程序上下文以包含这些bean定义
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>