applicationContext.xml配置

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:component-scan base-package="com.donghua"></context:component-scan>
<!-- 加载外部的配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 事物管理,配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<!-- Connection properties -->
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
<!-- Pool properties-->
<property name="minPoolSize" value="2" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="50" />
<property name="idleConnectionTestPeriod" value="3000" />
<property name="loginTimeout" value="300" />

</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--
<property name="mappingResources">
<list>
<value>com/donghua/bean/Article.hbm.xml</value>
<value>com/donghua/bean/BlogInfo.hbm.xml</value>
<value>com/donghua/bean/ClickNum.hbm.xml</value>
<value>com/donghua/bean/Critique.hbm.xml</value>
<value>com/donghua/bean/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>

</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>-->
<!-- 剩余的配置到。cfg.xml中找 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

</bean>

<!-- 配置声明式事物管理器  -->
<bean id="transctionManger"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- (基于注解的方式) -->
<tx:annotation-driven transaction-manager="transctionManger" />
</beans>

dao配置:注解配置

@Service
public class Dao {
@Resource
private SessionFactory sessionfactory;

@Transactional
public void addUser(User user){
Session session = sessionfactory.getCurrentSession();
session.save(user);

}
}

测试

public class BlogTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

@Test
public void testUser(){

User user = new User("赵六","sdfs","asf","fdd","dd");
Dao dao = (Dao) ac.getBean("dao");
dao.addUser(user);

}
}

解决No Hibernate Session bound to thread, and configuration does not allow creat。。。的更多相关文章

  1. No Hibernate Session bound to thread, and configuration does not allow creat

    No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...

  2. spring事务管理出错。No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy ...

  3. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a ...

  4. spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常

    java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not al ...

  5. Hibernate中解决No Hibernate Session bound to thread问题

    引用:忘了 首先是getCurrentSession()与openSession()的区别: 1.getCurrentSession()与openSession()的区别? * 采用getCurren ...

  6. 报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotatio ...

  7. 出现No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here异常

    问题描述: public void save(BaseEntity baseEntity) { Session session = null; try { session = currentSessi ...

  8. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a

    如果出现了这个错误,看看在使用Hibernate的时候有没有在事务的环境下执行操作!

  9. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not

    遇到这个问题之前,我去百度和谷歌去搜索了一下.发现各种说法.可是针对我的项目而言,也就是公司的项目而言,这个问题的根源并不是是网上所说的那样. 最后是通过自己的想法做測试得到了解决. 1.首先说说我的 ...

随机推荐

  1. rsyslog ~ 波浪号

    <pre name="code" class="html">Using negation can be useful if you would li ...

  2. c++链接数据库测试,中文有问题

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <Windows.h& ...

  3. android高仿微信拍照、多选、预览、删除(去除相片)相冊功能

    先声明授人与鱼不如授人与渔,仅仅能提供一个思路,当然须要源代码的同学能够私下有偿问我要源代码:QQ:508181017 工作了将近三年时间了,一直没正儿八经的研究系统自带的相冊和拍照,这回来个高仿微信 ...

  4. 学习ExtjsFor.NET(第二个案例-Array的Every方法)

    Ext.Array.every(Array array,Function fn,Object scope)是一个遍历的方法. array是数组,fn是方法,scope是作用域.every返回true和 ...

  5. 转发年浩大神的spfa算法

    http://www.cnblogs.com/superxuezhazha/p/5426624.html #include<iostream> #include<stdio.h> ...

  6. matlab GUI之常用对话框(四)-- 输入对话框 inputdlg、目录对话框 uigetdir、列表对话框 listdlg

    常用对话框(四) 1.输入对话框  inputdlg answer = inputdlg(prompt) answer = inputdlg(prompt,dlg_title) answer = in ...

  7. Method Swizzle黑魔法,修改 ios 系统类库方法 SEL IMP

    Method Swizzle黑魔法,修改 ios 系统类库方法   版权声明:本文为博主原创文章,未经博主允许不得转载. 一般来说,系统提供的方法已经足够开发了,但是有的时候有些需求用普通方法不好做. ...

  8. setTimeOut和setInterval详解

    setTimeout和setInterval的语法相同.它们都有两个参数,一个是将要执行的代码字符串,还有一个是以毫秒为单位的时间间隔,当过了那个时间段之后就将执行那段代码.不过这两个函数还是有区别的 ...

  9. socket(套接字)

    客户端: 创建套接字(socket) 连接服务器(connect) 通信(send,recv或者write,read) 关闭套接字(closesocket) 示例代码: int main(int ar ...

  10. MySQL varchar和char类型

    varchar和char是两种最主要的字符串类型.不幸的是,很难精确地解释这些值是怎么储存在磁盘和内存中的,因为这根存储引擎的具体实现有关.下面的描述假设使用的存储引擎是InnoDB或者MyISAM. ...