最近在自己捣鼓这个东西,使用的spring-framework-3.2.0 和 hibernate4.1。
想自己写个通用dao,结果在getCurrentSession()的时候报错了。但是使用openSession()是正常的。
请教下各位OSCER,这是什么情况,谢谢。
/** * Hibernate Session工厂 */ @Autowired @Qualifier("sessionFactory") private SessionFactory sessionFactory; protected Session d(){ return sessionFactory.getCurrentSession(); }下面是配置信息
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 数据源管理 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/MyDB?useUnicode=true&characterEncoding=UTF-8" /> <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="packagesToScan" value="com.beiyu" /> <property name="hibernateProperties"> <props> <prop key="current_session_context_class">thread</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">true</prop> <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> --> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="baseServiceAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT--> <tx:method name="find*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT--> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="remove*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <!--默认其他方法都是REQUIRED--> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 组件扫描路径 --> <context:component-scan base-package="com.beiyu.*" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/classes/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
在网上找了很多资料,有的说是要加
<prop key="current_session_context_class">thread</prop>
但是加了也不可以。。。。
在线等各位回答,谢谢!
<beanid="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<tx:adviceid="txAdvice"transaction-manager="txManager">
<tx:attributes>
<tx:methodname="save*"propagation="REQUIRED"/>
<tx:methodname="add*"propagation="REQUIRED"/>
<tx:methodname="create*"propagation="REQUIRED"/>
<tx:methodname="insert*"propagation="REQUIRED"/>
<tx:methodname=""read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:configproxy-target-class="true">
<aop:advisoradvice-ref="txAdvice"pointcut="execution(net.noday..service...(..))"/>
</aop:config>
这样才是完整的事物配置,你少了aop的配置所以没有事物
我没加这一段:<aop:configproxy-target-class="true"><aop:advisoradvice-ref="txAdvice"pointcut="execution( net.noday..service... (..))"/></aop:config>回去了试试。谢谢没事物也会这样
回复<aclass='referer'target='_blank'>@李烈火:你是说,在protectedSessiond(){}上面加上事务注解?这个方法我试了,同样的不行那就加上事物,curentsession必须在事物中是的啊。没使用事务呢。<spanstyle="font-family:Arial;font-size:14px;line-height:26px;background-color:#FFFFFF;">getCurrentSession需要做如下配置:
<spanstyle="font-family:Arial;font-size:14px;line-height:26px;background-color:#FFFFFF;">
<preclass="brush:xml;toolbar:true;auto-links:false;"><propertyname="current_session_context_class">thread</property><spanstyle="font-family:Arial;font-size:14px;line-height:26px;background-color:#FFFFFF;">强烈推荐看看这篇文章:
<spanstyle="font-family:Arial;font-size:14px;line-height:26px;background-color:#FFFFFF;"><atarget="_blank"rel="nofollow">HibernateSessionFactory中openSession和getCurrentSession方法的区别
回复<aclass='referer'target='_blank'>@汉唐:我先看下这篇文章,谢谢已经加了的,但是还不可以。<prop<spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:14.649999618530273px;background-color:#FFFFFF;"> key="current_session_context_class">thread</prop>
以上这段一定要去掉,然后配置文件加入如下(com.dao用你的dao包代替):
<preclass="brush:xml;toolbar:true;auto-links:false;"><aop:configexpose-proxy="true"><aop:pointcutid="txPointcut"expression="execution(com.dao...(..))"/><aop:advisoradvice-ref="baseServiceAdvice"pointcut-ref="txPointcut"/></aop:config></aop:config>
这样才是完整的事物配置,你少了aop的配置所以没有事物
<imgsrc="http://www.oschina.net/js/ke/plugins/emoticons/images/54.gif"alt=""/>,我也碰到这个了,但是aop我配置了的,求指教在serviceImpl层加@Transactional
</aop:config>
谢谢
Hibernate4方式改变了下,如果不指定@Transactional,需要在web.xml中指定
<!--openSessionInView配置-->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>.html</url-pattern>
</filter-mapping>
web.xml未配置openSession。
<!--配置Session-->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。