Hibernate事务处理

抛异常的代码:

((Long) getHibernateTemplate().iterate(query).next()).intValue()
异常信息:
Caused by: org.hibernate.exception.GenericJDBCException: could not get next iterator result
	at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
	at org.hibernate.impl.IteratorImpl.next(IteratorImpl.java:161)
	at com.yotoo.crm.dao.VisitLogDaoImp.findCountByCriteria(VisitLogDaoImp.java:98)
	at com.yotoo.crm.service.impl.VisitLogServiceImp.findAllVisitLog(VisitLogServiceImp.java:46)
	at com.yotoo.crm.service.impl.VisitLogServiceImp$$FastClassByCGLIB$$4353dcb1.invoke(<generated>)
	at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
	at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
	... 34 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 结果集已关闭。
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
	at com.microsoft.sqlserver.jdbc.SQLServerResultSet.checkClosed(SQLServerResultSet.java:211)
	at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getLong(SQLServerResultSet.java:2058)
	at org.apache.commons.dbcp.DelegatingResultSet.getLong(DelegatingResultSet.java:278)
	at org.apache.commons.dbcp.DelegatingResultSet.getLong(DelegatingResultSet.java:278)
	at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:61)
	at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
	at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
	at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:249)
	at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:229)
	at org.hibernate.impl.IteratorImpl.next(IteratorImpl.java:140)
	... 41 more
问题原因:

出现错误的原因是没有进行事务的配置,resultSet会关闭掉。

解决方法:
第一步:在spring的配置文件中添加事务的配置(如果你已经添加,那就不用了)
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
到这里spring的配置就完成了

第二步骤:在dao的上一层,即哪个类调用了刚才出错的语句的那个类添加事务,即在class前面添加一个注解:@Transaction

例子如下:
@Service("fileManageService")
@Transactional
public class FileManageServiceImpl implements IFileManageService{

@Resource
private IFileDAO fileDAO;

或者直接用xml配置,参考如下:

<!-- Transaction Manager -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- AOP -->
	<!-- Define exceptions -->
	<bean id="fileManageService" class="com.yotoo.nbi.service.impl.FileManageServiceImpl"/>
	<!-- Transaction Manager -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
            <tx:method name="update*" read-only="false" propagation="REQUIRED" />
            <tx:method name="delete*" read-only="false" propagation="REQUIRED" />
            <tx:method name="save*" read-only="false" propagation="REQUIRED" />
            <tx:method name="*"  propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="txPointcut" expression="bean(fileManageService)" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
	</aop:config>
事务配置好之后就不会出现那个bug了

转载于:https://my.oschina.net/yotoo/blog/264341

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值