Could not obtain transaction-synchronized Session for current thread -无法获取当前线程的事务同步会话-解决方案
2016-12-27 10:25
525 查看
@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无法自动提交的问题,试一试我的解决方案,说不定能解决出来哦
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 解决方案
- 事务配置不对导致:•Could not obtain transaction-synchronized Session for current thread
- 由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原因及解决方案
- SpringMVC中Could not obtain transaction-synchronized Session for current thread的解决方案
- SpringMVC中Could not obtain transaction-synchronized Session for current thread的解决方案
- Could not obtain transaction-synchronized Session for current thread
- springmvc+spring4+hibernate4整合Could not obtain transaction-synchronized Session for current thread
- Could not obtain transaction-synchronized Session for current thread
- SSH整合时出现Could not obtain transaction-synchronized Session for current thread
- 关于Hibernate Could not obtain transaction-synchronized Session for current thread
- Hibernate4 提示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
- 【Hibernate】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