admin管理员组文章数量:1612097
使用Spring+Hibernate,获取数据库Session时,总是抛出异常:No Session found for current thread。
在网卡找了好几天,终于解决
http://blog.csdn/greensurfer/article/details/8982876
现将我自己的配置贴出来
Spring配置文件spring.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework/schema/beans"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance" xmlns:aop="http://www.springframework/schema/aop"
xmlns:context="http://www.springframework/schema/context"
xmlns:tx="http://www.springframework/schema/tx"
xsi:schemaLocation="http://www.springframework/schema/beans
http://www.springframework/schema/beans/spring-beans-4.0.xsd
http://www.springframework/schema/aop
http://www.springframework/schema/aop/spring-aop-4.0.xsd
http://www.springframework/schema/context
http://www.springframework/schema/context/spring-context-4.0.xsd
http://www.springframework/schema/tx
http://www.springframework/schema/tx/spring-tx-4.0.xsd">
<context:annotation-config />
<context:component-scan base-package="th05" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apachemons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.93.200:3306/kangpy" />
<property name="username" value="admin_kangpy" />
<property name="password" value="admin123" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
<!-- 配置session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 这里需要注意在hibernate3中,使用 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean -->
<property name="dataSource" ref="dataSource" /> <!-- session所依赖的数据源 -->
<!-- 定义持久化类所在的包 -->
<!--<property name="packagesToScan" value="th05.hibernate" /> 这是一种方法 -->
<!-- 以下是另一种方式 -->
<property name="packagesToScan">
<!-- 定义了此,但双没有任何实际的类,会抛出异常:
java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration -->
<list>
<value>th05.hibernate</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.HSQLDialect</prop>
<!--
抛出此异常时,No Session found for current thread
需要进行添加以下这一句,另外,还需要
tx:annotation-driven transaction-manager="transactionManager
-->
<prop key="<span style="color:#FF0000;">hibernate.current_session_context_class</span>">org.springframework.orm.hibernate4.SpringSessionContext</prop>
</props>
</property>
</bean>
<!-- 事务配置 ,此不是必须的,只有需要支持事务控制的工程在才需要,不过一般都需要,所以一般都需要配置这个 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" /> <!-- 事务中获取session时,是从这里获取的 -->
</bean>
<!-- 定义事务管理器,此是告诉Spring检查上下文中所有的Bean并查找使用@Transactional注解的Bean,而不管这个注解是用在类级别还是方法级别上
对每一个使用@Transactional注解的Bean,<tx:annotation-driven>会自动 为它添加事务通知。
在类级别上使用@Transactional注解,表示所有的方法都支持事务并且是只读的
-->
<<span style="color:#FF0000;">tx:annotation-driven transaction-manager</span>="transactionManager"/>
<bean id="aaabbb" class="th05.hibernate.CustomerDao" />
<bean id="aaabbb1" class="th05.hibernate.CustomerDao1" />
</beans>
//测试代码如下
package th05.hibernate;
import java.util.List;
import kpy.SpringInAction.LogWriter;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class CustomerDao1{
private SessionFactory sessionFactory;
@Autowired
public void setMySessionFactory(SessionFactory sessionFactory){
LogWriter.trace(LogWriter.getMethodName(this.getClass().getName()));
//System.out.println("sssssssssssssssssssss " + (Object)sessionFactory);
this.sessionFactory = sessionFactory;
}
private Session currentSession (){
return this.sessionFactory.getCurrentSession();
}
@Transactional
public Customers getObjById (String id) {
LogWriter.trace(LogWriter.getMethodName(this.getClass().getName()));
Customers c = null;
try{
Session session = this.currentSession();
// get的第二个参数为表的主键
c = (Customers)session.get(Customers.class, new String("32363240"));
c.show();
}catch(Exception e) {
System.out.println(e);
}
return c;
}
}
版权声明:本文标题:终于解决了:No Session found for current thread 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728630151a1166991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论