业务:通过spring管理数据源,实现用户注册。
问题描述:执行hibernateTemplate.save(obj);时报错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.
错误分析:1.可能是sessionFactory的注入有问题
2.如错误所示FlushMode设置错误。
小弟这个问题已经困扰2天了,有点想拿头撞墙,扒光头发的感觉!!求大神帮忙!!代码如下~
spring配置文件:applicationContext2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 定义数据源的信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/project" />
<property name="user" value="root" />
<property name="password" value="gg123" />
<property name="maxPoolSize">
<value>80</value>
</property>
<property name="minPoolSize">
<value>1</value>
</property>
<property name="initialPoolSize">
<value>1</value>
</property>
<property name="maxIdleTime">
<value>20</value>
</property>
</bean>
<!--定义Hibernate的SessionFactory -->
<!-- SessionFactory使用的数据源为上面的数据源 -->
<!-- 指定了Hibernate的映射文件和配置信息 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref ="dataSource" />
<property name="mappingResources">
<list>
<value>com/easi/beans/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="baseDao" class="com.easi.daoImpl.UserDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--用户注册业务逻辑类 -->
<bean id="userManager" class="com.easi.serviceImpl.UserManagerImpl">
<property name="dao">
<ref bean="baseDao" />
</property>
</bean>
<!-- 用户注册的Action -->
<bean id="regAction" class="com.easi.action.RegisterAction">
<property name="userManager">
<ref bean="userManager" />
</property>
</bean>
//hibernateTemplate注入
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Registeraction.java 由struts拦截的action处理类
public class RegisterAction extends ActionSupport {
private static final long serialVersionUID = 1L;
//UserForm类是bean类,作用是接受前台的orm中的注册信息
private UserForm user;
public UserForm getUser() {
return user;
}
public void setUser(UserForm user) {
this.user = user;
}
private UserManager userManager;
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
public String execute() {
try {
userManager.regUser(user);
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}
UserManagerImpl 业务实现类
public class UserManagerImpl implements UserManager {
private BaseDao dao;
public void setDao(BaseDao dao) {
this.dao = dao;
}
@Override
public void regUser(UserForm userForm) {
User user = new User();
//此方法将userForm中的值传给user
BeanUtils.copyProperties(userForm,user);
System.out.println("接受到的用户名:"+user.getUserName());
System.out.println("接受到的密码:"+user.getPassword());
System.out.println("接受到的性别:"+user.getGender());
dao.saveObject(user);
}
}