`
aumy2008
  • 浏览: 117863 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring2.0事务配置问题(重写父类方法后出错)

阅读更多

由于一些功能模块用到的Service和DAO层的方法类似,现将类似的方法进行重构,提出基本的Service和DAO,真正用到的Service和DAO只要实现或者继承即可。在重构后遇到事务配置问题,具体如下。

 

抛出异常:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
 at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1085)
 at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:624)
 at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:362)
 at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:622)

 

 

Spring2.0配置:

	<aop:config>
		<aop:advisor
			pointcut="execution(* aumy2008.service.impl..*.*(..)) "
			advice-ref="txAdviceRet" />
	</aop:config>

	<tx:advice id="txAdviceRet"
		transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"
				propagation="REQUIRED" />
			<tx:method name="load*" read-only="true"
				propagation="REQUIRED" />
			<tx:method name="find*" read-only="true"
				propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

 

重构后的基本接口和实现类的部分代码:

 

public interface IBaseService<T> {

	public Long add(T t);

	public void modify(T t);

	public void addOrModify(T t);

	......
}

 

public class BaseService<T> implements IBaseService<T> {

	private IRBaseDao<T> irBaseDao;

	private boolean isSwitchCharsetTranslate;

	public BaseService(IRBaseDao<T> irBaseDao, boolean isSwitchCharsetTranslate)
			throws Exception {
		if (irBaseDao == null) {
			throw new Exception("irBaseDao may not be null");
		}
		this.irBaseDao = irBaseDao;
		this.isSwitchCharsetTranslate = isSwitchCharsetTranslate;
	}


	public Long add(T t) {
		return (Long) this.irBaseDao.save(t);
	}


	public void addOrModify(T t) {
		this.irBaseDao.saveOrUpdate(t);
	}

	public void modify(T t) {
		this.irBaseDao.update(t);
	}

	......
}

 

public interface IRBaseDao<T> {

	public Serializable save(Object o);	

	public void update(Object o);
	
	public void saveOrUpdate(Object o);

	......
}
 
public class RBaseDao<T>  extends HibernateDaoSupport implements IRBaseDao<T> {

	public Serializable save(Object o) {
		Serializable id = getHibernateTemplate().save(o);
		this.flush();
		return id;
	}

	public void update(Object o) {		
		getHibernateTemplate().update(o);
		this.flush();
	}
	
	public void saveOrUpdate(Object o) {
		getHibernateTemplate().saveOrUpdate(o);
		this.flush();
	}

	......
}

 具体业务的接口和实现类的部分代码:

public interface ITypeService extends IBaseService<Type> {

}

 

public class TypeService extends BaseService<Type> implements ITypeService {
	private IRBaseDao<Type> typeDAO;

	public boolean isSwitchCharsetTranslate;
	public TypeService(IRBaseDao<Type> typeDAO, boolean isSwitchCharsetTranslate)
			throws Exception {
		super(typeDAO, isSwitchCharsetTranslate);
		this.typeDAO = typeDAO;
		this.isSwitchCharsetTranslate = isSwitchCharsetTranslate;
	}

	public void addOrModify(Type t) {
		typeDAO.saveOrUpdate(toDBValue(t));
	}

	public Long add(Type t) {
		return (Long) typeDAO.save(toDBValue(t));
	}

	public void modify(Type t) {
		typeDAO.update(toDBValue(t));
	}

	private Type toDBValue(Type type) {
		......
		return type;
	}

}
 
public interface ITypeDAO extends IBaseDao<Type> {

}
 
public class TypeDAO extends RBaseDao<Type> implements ITypeDAO {

}


如果TypeService不重写父类的方法,运行正常
代码如:

public class TypeService extends BaseService<Type> implements ITypeService {

	public TypeService(IRBaseDao<Type> typeDAO, boolean isSwitchCharsetTranslate)
			throws Exception {
		super(typeDAO, isSwitchCharsetTranslate);
	}

}

 

我现在就是想在TypeService类中实现字符集的转换,需要重写add等三个方法,当重写后,运行就报开始给出的异常。
分享到:
评论
3 楼 aumy2008 2008-01-09  
用@Transactional(propagation = Propagation.SUPPORTS)
@Transactional(propagation = Propagation.REQUIRED)开启了子类的事务,具体代码如下:
@Transactional(propagation = Propagation.SUPPORTS)
public class TypeService extends BaseService<Type> implements ITypeService {
private IRBaseDao<Type> typeDAO;

public boolean isSwitchCharsetTranslate;
public TypeService(IRBaseDao<Type> typeDAO, boolean isSwitchCharsetTranslate)
throws Exception {
super(typeDAO, isSwitchCharsetTranslate);
this.typeDAO = typeDAO;
this.isSwitchCharsetTranslate = isSwitchCharsetTranslate;
}
        
         @Transactional(propagation = Propagation.REQUIRED)
public void addOrModify(Type t) {
typeDAO.saveOrUpdate(toDBValue(t));
}

         @Transactional(propagation = Propagation.REQUIRED)
public Long add(Type t) {
return (Long) typeDAO.save(toDBValue(t));
}

         @Transactional(propagation = Propagation.REQUIRED)
public void modify(Type t) {
typeDAO.update(toDBValue(t));
}

private Type toDBValue(Type type) {
......
return type;
}

}
2 楼 aumy2008 2008-01-09  
谢谢 xly_971223 !
问题解决了。
1 楼 xly_971223 2008-01-04  
name 写道
  1. <aop:config> 
   2.     <aop:advisor 
   3.         pointcut="execution(* aumy2008.service.impl..*.*(..)) " 
   4.         advice-ref="txAdviceRet" /> 
   5. </aop:config> 
   6.  
   7. <tx:advice id="txAdviceRet" 
   8.     transaction-manager="transactionManager"> 
   9.     <tx:attributes> 
  10.         <tx:method name="get*" read-only="true" 
  11.             propagation="REQUIRED" /> 
  12.         <tx:method name="load*" read-only="true" 
  13.             propagation="REQUIRED" /> 
  14.         <tx:method name="find*" read-only="true" 
  15.             propagation="REQUIRED" /> 
  16.         <tx:method name="*" propagation="REQUIRED" /> 
  17.     </tx:attributes> 
  18. </tx:advice> 

改成传统事务配置试试
org.springframework.transaction.interceptor.TransactionProxyFactoryBean

相关推荐

    Spring2.0 事务处理

    NULL 博文链接:https://todd-liangt.iteye.com/blog/337274

    spring2.0技术手册

    林信良著 spring2.0技术手册 《Spring 2.0技术手册》介绍了Spring 2.0的新特性,诸如Spring 2.0的新配置、新AOP支持、增强的IoC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与...

    Spring 2.0 spring 2.0 标准API

    Spring 2.0 标准API 用处不大的资源我不发

    Spring2.0的配置

    看着有用就自己下载吧

    Spring2.0jar包

    Spring2.0jar包,J2EE项目中,用于集成的框架。

    Spring 2.0 中文参考手册

    Spring 2.0 中文参考手册,Spring 2.0 中文参考手册,Spring 2.0 中文参考手册,Spring 2.0 中文参考手册Spring 2.0 中文参考手册,

    Spring2.0技术手册

    本书介绍了Spring 2.0的新特性,诸如Spring 2.0的新配置、新AOP支持、增强的IoC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与Spring相关API的使用技巧,能够显著减少每一位入门者...

    Spring2.0技术手册(林信良)_part2

    本书介绍了Spring 2.0的新特性,诸如Spring 2.0的新配置、新AOP支持、增强的IoC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与Spring相关API的使用技巧,能够显著减少每一位入门者...

    Spring2.0的配置2

    接着spring2.0的配置后写的东西

    详尽的Spring2.0学习提纲

    对于学习Spring2.0很好的指导作用,给初学者指明一个学习的方向和思路。

    spring2.0jar包(一)

    spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一)

    SPRING2.0开发详解

    SPRING2.0开发详解: 详细阐述了SPRING2.0的核心技术和框架整合能力

    spring2.0 中文帮助文档 pdf

    spring2.0 中文帮助文档 spring2.0 中文帮助文档 spring2.0 中文帮助文档 spring2.0 中文帮助文档 spring2.0 中文帮助文档

    spring2.0 使用 Quartz 必备包

    spring2.0 使用 Quartz 必备包 里面有: quartz-all-1.5.2.jar spring-support.jar 适合:SSH框架 spring2.0 strut2.0 hibernate3.2等

    Spring2.0中文教程

    Spring2.0中文教程

    spring2.0技术手册 (扫描版)

    第1章认识Spring 第2章Spring入门 第3章Bean、消息、事件 第4章SpringAOP 第5章JDBC、事务支持 第6章Hibernate与Spring 第7章SpringWebMVC框架 第8章View层方案、Web框架整合 第9章API封装 第10章项目:...

    spring2.0(中文)

    本书介绍了Spring2.0的新特性,诸如Spring2.0的新配置、新AOP支持、增强的IOC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与Spring相关API的使用技巧,能够显著减少每一位入门者摸索...

    spring2.0中文文档

    spring2.0中文文档

    spring2.0中文手册及使用指南 chm

    spring2.0中文手册及使用指南,chm格式

    Spring2.0宝典源代码

    Spring2.0宝典全书源代码,作者李刚,随书所附的光盘

Global site tag (gtag.js) - Google Analytics