错误是这样的:
当我单独测试dao的时候,会报错:No Session found for current thread
但是把到加入到service时,再测试,就没问题
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:annotation-config/>
<context:component-scan base-package="com.zq"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialSize" value="10"></property>
<property name="maxActive" value="1000"></property>
<property name="maxIdle" value="300"></property>
<property name="minIdle" value="10"></property>
<property name="maxWait" value="10000"></property>
<!--<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60"/>
<property name="logAbandoned" value="true" />
<property name="testOnBorrow" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="select getdate()"/>-->
</bean>
<!--整合hibernate-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>-->
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</prop>
</props>
</property>
<property name="packagesToScan">
<value>com.zq.po</value>
</property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 对get/find/search开头的方法要求只读事务 -->
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="search*" propagation="REQUIRED" read-only="true"/>
<tx:method name="read*" propagation="REQUIRED" read-only="true"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="count*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="list*" propagation="REQUIRED" read-only="true"/>
<!-- 对其它方法要求事务 -->
<tx:method name="*" propagation="REQUIRED"/> </tx:attributes>
</tx:advice>
<!--声明一个切面-->
<aop:config>
<aop:pointcut id="daoMethods" expression="execution(* com.zq.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods"/>
</aop:config>
</beans>
请问这是怎么回事呢,再有就是我这个配置写的规范吗?为什么不能回滚事务呢?hibernatespring
当我单独测试dao的时候,会报错:No Session found for current thread
但是把到加入到service时,再测试,就没问题
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:annotation-config/>
<context:component-scan base-package="com.zq"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialSize" value="10"></property>
<property name="maxActive" value="1000"></property>
<property name="maxIdle" value="300"></property>
<property name="minIdle" value="10"></property>
<property name="maxWait" value="10000"></property>
<!--<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60"/>
<property name="logAbandoned" value="true" />
<property name="testOnBorrow" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="select getdate()"/>-->
</bean>
<!--整合hibernate-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>-->
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</prop>
</props>
</property>
<property name="packagesToScan">
<value>com.zq.po</value>
</property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 对get/find/search开头的方法要求只读事务 -->
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="search*" propagation="REQUIRED" read-only="true"/>
<tx:method name="read*" propagation="REQUIRED" read-only="true"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="count*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="list*" propagation="REQUIRED" read-only="true"/>
<!-- 对其它方法要求事务 -->
<tx:method name="*" propagation="REQUIRED"/> </tx:attributes>
</tx:advice>
<!--声明一个切面-->
<aop:config>
<aop:pointcut id="daoMethods" expression="execution(* com.zq.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods"/>
</aop:config>
</beans>
请问这是怎么回事呢,再有就是我这个配置写的规范吗?为什么不能回滚事务呢?hibernatespring
解决方案 »
- Error creating bean with name 'sessionFactory' defined in class path resource...
- tomcat应用,log4j日志的配置
- 刚培训完J2EE,进个什么样的公司好啊?各位前辈给点意见。
- oracle驱动兼容问题
- 那里错了啊。求助!
- 那位大哥快帮帮我,在线急等!一个简单的spring MVC入门问题
- 闷啊!!!servlet包应该放到哪里????
- 请问在Java中,如何编写一个菜单来调用html页面?
- 为什么J2EE的部署和发布 与dephi,C++如此不同??知道的请回答
- 关于IO字符流遇到的问题
- java平台上,类似于WCF的技术,一般用什么?
- xml schema的相关问题请教,谢谢!
<tx:attributes>
<tx:method name="*" rollback-for="Throwable" no-rollback-for="InstrumentNotFoundException"/>
</tx:attributes>
</tx:advice>楼主先试试
<tx:attributes>
<tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>