Could not obtain transaction-synchronized Session for current thread -无法获取当前线程的事务同步会话-解决方案
来源:互联网 发布:sql 分组小计合计 编辑:程序博客网 时间:2024/04/27 17:37
@Repository("nameimpl")
public class nameimpl implements namedao {
@Resource(name="sessionFactory")
private SessionFactory sessionfactory;
public Session getsession(){
return sessionfactory.getCurrentSession();
}
@Override
public int delete(int id) {
Name n=(Name) this.getsession().get(Name.class, id);
this.getsession().delete(n);
System.out.println("再次进入删除方法!");
return 0;
}
@Override
public List<Name> getall() {
String sql = "from entity.Name";
List<Name> names = this.getsession().createQuery(sql).list();
return names;
}
}
这时我的dao代码,我使用的sessionfactory.getCurrentSession();
我建议使用getCurrentSession(),而不是opensession,因为opensession不能自动关闭,还会报其他错误,也挺麻烦的。
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 允许 Hibernate 针对特定的关系数据库生成优化的 SQL 的 org.hibernate.dialect.Dialect
的类名。 例如:org.hibernate.dialect.MySQLDialect -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行 -->
<!-- ,hibernate.hbm2ddl.auto可以帮助你实现正向工程,即由java代码生成数据库脚本,进而生成具体的表结构. -->
<property name="hibernate.show_sql">true</property><!-- 输出所有 SQL 语句到控制台。 -->
<property name="hibernate.format_sql">true</property><!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->
<!-- 配置 hibernate 二级缓存相关的属性. -->
<!-- <property name="current_session_context_class" >thread</property> -->
</session-factory>
</hibernate-configuration>
这是我的hibernate.cfg.xml,按理说要使用 sessionfactory.getCurrentSession();
就必须<property name="current_session_context_class" >thread</property>
但是,我的经验总结,必须把<property name="current_session_context_class" >thread</property>这句话注释掉,否者会报错
@Transactional
@Service("nameservice")
public class nameservice{
@Resource(name="nameimpl")
private nameimpl nameimpl;
public int delete(int id) {
System.out.println("进入删除的方法!");
return nameimpl.delete(id);
}
public List<Name> getall() {
System.out.println("进查询的方法!");
return nameimpl.getall();
}
}
这是我的service,注意:service上面要加@Transactional,不要加在dao,上面,加在service上面
光写一个@Transactional是没有用的
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="globalRollbackOnParticipationFailure" value="false" />
</bean>
<!-- 2. 配置事务属性, 需要事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" /> <!-- <tx:method name="read*" read-only="true" /> <tx:method name="list*"
read-only="true" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method
name="modify*" propagation="REQUIRED" /> -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 3. 配置事务切点, 并把切点和事务属性关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* hibernate.*.*(..))"
id="txPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
必须要在配置文件中加<tx:annotation-driven transaction-manager="transactionManager"/>这句话
最后:要在web.xml中加这一段代码
<filter>
<filter-name>hibernateFilter</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>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我的springmvc+hibernate整合问题解决了,希望大家也能解决出来,一般都是transaction无法自动提交的问题,试一试我的解决方案,说不定能解决出来哦
- Could not obtain transaction-synchronized Session for current thread -无法获取当前线程的事务同步会话-解决方案
- SpringMVC中Could not obtain transaction-synchronized Session for current thread的解决方案
- SpringMVC中Could not obtain transaction-synchronized Session for current thread的解决方案
- 【Hibernate】Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- HibernateException: Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- Ubuntu操作系统安装之开发人员必备
- 使用Mapreduce将hbase表中的数据全量导入ElasticSearch
- 非常全的linux面试笔试题及参考答案
- java 栈的isEmpty()和empty()有什么区别
- 我的表弟是个游戏狂热分子,可他最近…
- Could not obtain transaction-synchronized Session for current thread -无法获取当前线程的事务同步会话-解决方案
- onload、onunload 和 onbeforeunload的区别
- Android系统全貌
- java.util.Date和java.sql.Date的区别和相互转化
- GET和POST的一些小事
- python错误:IndentationError: unexpected indent
- struts2验证码
- PHPStorm内嵌浏览器配置教程
- 【Android】灵云语音合成