解决No Hibernate Session bound to thread, and configuration does not allow creat。。。
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。。。的更多相关文章
- No Hibernate Session bound to thread, and configuration does not allow creat
No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...
- 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 ...
- 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 ...
- 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 ...
- Hibernate中解决No Hibernate Session bound to thread问题
引用:忘了 首先是getCurrentSession()与openSession()的区别: 1.getCurrentSession()与openSession()的区别? * 采用getCurren ...
- 报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotatio ...
- 出现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 ...
- org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a
如果出现了这个错误,看看在使用Hibernate的时候有没有在事务的环境下执行操作!
- org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not
遇到这个问题之前,我去百度和谷歌去搜索了一下.发现各种说法.可是针对我的项目而言,也就是公司的项目而言,这个问题的根源并不是是网上所说的那样. 最后是通过自己的想法做測试得到了解决. 1.首先说说我的 ...
随机推荐
- 计算机世界的道(C/ASM)生一(OS),一生二(API),二生万象(MFC/COM)——学包装技术的程序员将来会损失比较大,因为不了解本质,一旦包装过时就会被淘汰
道生一,一生二,二生万象.OO的思想就是抽象,万象归宗,化繁为简.99%的程序员使用OO,或者所谓的类库的目的就是好用,不必了解内部实现就可以直接达到所期望的结果.这时一种生产力的进步,一种流水线式半 ...
- Day4_代码重用与函数
知识点速记: 重用代码的方法:脚本包含require().include(); 全局配置文件php.ini(auto_prepend_file/auto_append_file); 目录配置文件.ht ...
- Joseph(约瑟夫环)
Joseph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Swift与Objective-C交互
在同一个工程中是可以同时使用Swift和OC的,但不可以同时出现在同一个文件中. OC调用Swift相关信息的方法 在***.m文件中导入工程名-Swift.h即可. 如工程名为ABC,则在需要使用S ...
- 通过 CsvListWriter 读写.csv文件辅助类
package cn.gov.cnis.db; import java.io.File; import java.io.FileReader; import java.io.FileWriter; i ...
- Nio Server
package org.fxc.nio.server; import java.io.FileInputStream; import java.io.IOException; import java. ...
- [C#参考]事件机制
还是那个项目,为了降低程序的耦合性,我决定小小的重构一下自己原来的代码,把Socket通信和帧的分析这两部分分别封装成一个类,当然线程没有变,只是封装了一下,为的就是模块测试完容易拼接.这也是我打算降 ...
- Java学习之String对象为什么是不可变的
转自:http://www.2cto.com/kf/201401/272974.html,感谢作者的总结 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变 ...
- java——多线程——单例模式的static方法和非static方法是否是线程安全的?
单例模式的static方法和非static方法是否是线程安全的? 答案是:单例模式的static方法和非static方法是否是线程安全的,与单例模式无关.也就说,如果static方法或者非static ...
- adb wifi连接手机
1. 默认情况下,ADB是通过USB来进行连接的. 不需要USB线,直接在android设备上安装一个超级终端,在终端里运行以下代码即可: su setprop service.adb.tcp.por ...