`

SSH中使用getCurrentSession()获得session

阅读更多

SSH中使用getCurrentSession()获得session

 

  1. 在hibernate的配置文件中增加属性:

     

    <property name="current_session_context_class">thread</property>

    如下表红色部分

    <hibernate-configuration>
    
    <session-factory>
    
       <property name="connection.username">root</property>
    
       <property name="connection.url">
    
        jdbc:mysql://localhost:3306/dbtest
    
       </property>
    
       <property name="dialect">
    
        org.hibernate.dialect.MySQLDialect
    
       </property>
    
       <property name="myeclipse.connection.profile">dbtest</property>
    
       <property name="connection.password">123456</property>
    
       <property name="connection.driver_class">
    
        com.mysql.jdbc.Driver
    
       </property>
    
       <property name="show_sql">true</property>
    
      <property name="current_session_context_class">thread</property>
    
       <mapping resource="com/pc386/hibernate/entity/User.hbm.xml" />
    
      
    
    </session-factory>
    
    </hibernate-configuration>
     
    

     

    在SSH中,如果把hibernate交给Spring的管理事物中,那么应该修改Spring的配置文件applicationContext.xml文件的属性,在sessionFactory的属性中增加: <prop key="current_session_context_class">thread</prop>
    bean id="sessionFactory" 
    
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    
    <property name="dataSource">
    
       <ref local="dataSource"/>
    
    </property>
    
    <property name="mappingResources">
    
       <list>
    
        <value>User.hbm.xml </value>
    
       </list>
    
    </property>
    
    <property name="hibernateProperties">
    
       <props>
    
        <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    
    <!--
    
        <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
    
    -->
    
        <prop key="hibernate.current_session_context_class">thread</prop>
    
       </props>
    
    </property>
    
    </bean>
     
    
    
     原文出处
  2. http://blog.csdn.net/daryl715/archive/2007/02/11/1507385.aspx
    No CurrentSessionContext configured!" 异常解决方案
    
    hibernate 老说没有配方言
    
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    
    这句话明明就写在了配置文件里面,可老是没有写
    
    错误如下:
    
    Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
    
    name改成"hibernate.dialect"也不行
    
    Hibernate-Version: 3.1.1
    
    mysql -Verison: 4.0.16
    
    用这种方法正确
    
    static {
    
    try {
    
    configuration = new Configuration();
    
    sessionFactory = configuration.configure().buildSessionFactory();
    
    } catch (Throwable ex) {
    
    throw new ExceptionInInitializerError(ex);
    
    }
    
    }
    
    这种方式会出现异常
    
    // static {
    
    // try {
    
    // // Create a configuration based on the properties file we've put
    
    // Configuration config = new Configuration();
    
    // config.addClass(Customer.class).addClass(Order.class);
    
    // // Get the session factory we can use for persistence
    
    // sessionFactory = config.buildSessionFactory();
    
    // } catch (Exception e) {
    
    // e.printStackTrace();
    
    // }
    
    // }
    
    第二种方式是针对使用properties文件配置hiernate的写法,使用hibernate.cfg.xml应使用第一种调用方式
    
    或者在hibernate.cfg.xml中加入:
    
    <property name="current_session_context_class">thread</property>
    
     http://blog.sina.com.cn/s/blog_412d58ec010005p5.html
    以前单独用Hibernate2.0的时候,为了保证一个线程中每次取出的session都是一个对象,就使用官方提供的一个HibernateUtils,将第一次取出的session放入ThreadLocal中,以后每次从这里面取出的session都是一个对象,可以保证事务的正常执行。
    
    后来升级到3.0,也这样延用下去,没怎么关心3.0的新特性。
    
    前几天想将Hibernate加入到SPRING的事务管理中,但是我又不想使用spring提供的HibernateDaoSupport或HibernateTemplate,只想像以前那样在一个线程中的任意地方都能得到同一个session。这样就出问题了,spring管理事务的话,如果要保证当前线程内只有一个session,需要将sessionFactory传递给org.springframework.orm.hibernate3.HibernateTransactionManager,spring负责事务的开始,提交,回滚以及session的关闭,假设spring用于管理事务的session是(session1)。如果我还用HibernateUtils.getCurrentSession()方法获得session的话,得到的session却是(session2),和开始事务的session不是同一个对象,就造成session2的事务没有提交,对数据库的操作无效。
    
    后来搜索到原来Hibernate3.0以后,sessionFactory多了个新的方法getCurrentSession()。我心里很高兴,这样不就解决我的问题了吗?
    
    于是我就将HibernateUtils中的session=sessionFactory.openSession()方法改为了session=sessionFactory.getCurrentSession().
    
    运行测试竟然错了,
    
    提示什么TransactionManager出错了,上网上搜了搜,原来使用getCurrent方法必须配置事务管理,需要和jta或thread绑定。我不想用jta,当然要和线程绑定了。我在hibernate的配置文件中加入了<property name="current_session_context_class">thread</property>,又运行,还是出同样的错误。继续上网上查资料,基本没有相关资料。后来费了好大劲才发现,原来在hibernate3.0里只能和jta绑定,我copy了新的3.2的包,这下可以了。不出那个事务管理的错了,变成了org.hibernate.HibernateException: save is not valid without active transaction错误。继续搜,只有一个相关资料,说使用getCurrentSession()方法必须在事务中运行。不管查询还是更新操作都要开始事务,操作,提交事务才行。
    
    可是我已经配置了让spring管理事务,为什么出这个错误?看来只有一个原因,spring在通过sessionFactory获取session时,没有调用getCurrentSession()方法。
    
    自己手动加入事务的开始,提交,成功了。
    
    经过多次实验,发现,只能能spring中配置sessionFactory,才能让spring管理hibernate的事务。
    
    <bean id="sessionFactory"
    
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    
    <property name="dataSource">
    
       <ref local="dataSource"/>
    
    </property>
    
    <property name="mappingResources">
    
       <list>
    
        <value>User.hbm.xml </value>
    
       </list>
    
    </property>
    
    <property name="hibernateProperties">
    
       <props>
    
        <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    
    <!--
    
        <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
    
    -->
    
        <prop key="hibernate.current_session_context_class">thread</prop>
    
       </props>
    
    </property>
    
    </bean>
    
    在程序中要用到session的时候,过程如下 :
    
    UserDAOImpl:
    
    {
    
    SessionFactory sf = [从spring中得到sessionFactory];
    
    Session session = sf.getCurrentSession();
    
    //数据库操作
    
    }
    
    这么乱。写一下总结。
    
    1. 如果想让spring帮你管理事务,只能在spring中配置SessionFactory。如果使用hibernate原有的sessionFactory,则只能自己手动管理事务。
    
    2. 如果想使用sessionFactory.getCurrentSession()方法,必须配置sessionFactory和jta或thread绑定。但是hibernate3.0不支持与thread绑定,3.1以上才支持。
    
    3. sessionFactory.getCurrentSession()方法取得的session,在做数据库操作时必须在事务中做,包括只读的查询,否则会报错。
     
    
    
     
分享到:
评论

相关推荐

    ssh中getCurrentSession的使用

    ssh整合中getCurrentSession()的使用,个人写的练习,备忘用,不建议下载

    新Hibernate SessionFactory().getCurrentSession()猫腻

    NULL 博文链接:https://zgdkik.iteye.com/blog/1835667

    SessionFactory.getCurrentSession与openSession的区别

    博文链接:https://shaqiang32.iteye.com/blog/201918

    OA项目SSH整合框架

    2,但在加载时,如果Session已经关掉了就会抛LazyInitializationException异常 二,集成 Spring 与 Struts2.1.8.1 1,在web.xml配置监听器(Spring Reference 15.2 Common configuration) &lt;!-- 集成Spring -...

    getCurrentSession 与 openSession() 的区别

    NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085

    NHibernate Demo

    3.程式中使用只需要打开连接,不需要关闭 4.ISession session = NHibernateHelper.GetSession("HR"); 带参数的需要在Config中增加NHConfigSettings节,格式同AppSettings .GetCurrentSession();不带参数的情况下Web....

    basic-common2h3:基于hibernate3的BaseDao,完成增删改查、分页操作

    通过getCurrentSession方法获取session操作对象。 示例: public interface IUserDao extends IBaseDao { } 3、定义UserDao并实现IUserDao接口,且要实现BaseDao类还需要传入泛型。 BaseDao完成了具体的CRUD的方法。...

    mysql+jdbc+jsp+Hibernate3.2+tomcattomcat5.028成功测试

    Session session1 =sessionFactory.getCurrentSession(); session1.beginTransaction(); &lt;br&gt; String title="jkh" ; Date theDate= new Date(); Event theEvent = new Event(); theEvent....

    Java常见面试题208道.docx

    118.在 hibernate 中使用 Integer 和 int 做映射有什么区别? 119.hibernate 是如何工作的? 120.get()和 load()的区别? 121.说一下 hibernate 的缓存机制? 122.hibernate 对象有哪些状态? 123.在 hibernate 中 ...

    java 调用存储过程列子

    学生在学习jdbc的时候,会问到怎么调用存储过程,现在将java调用oracle存储过程的示例总结如下

    Hibernate5的Query接口浅析

    使用Query对象可以方便的查询数据库中的数据,它主要使用HQL或者本地SQL查询数据。Query对象不仅能查询数据,还可以绑定参数、限制查询记录数量、实现批量删除和批量更新等。 Configuration cfg=new Configuration()...

    Hibernate 修改数据的实例详解

    Session currentSession = H3Utils.getCurrentSession(); currentSession.beginTransaction(); //创建 HQL String hqlString = update Person p set p.name=? , p.age=? where p.id=?; //构建 Query Query ...

    Java面试宝典2020修订版V1.0.1.doc

    13、你是如何使用jquery中的ajax的? 27 14、jquery中的$.get和$.post请求区别? 27 15、jquery中如何操作样式的? 28 16、如何设置和获取HTML和文本的值? 28 17、Jquery能做些什么? 28 18、在ajax中data主要有哪...

    hibernate 学习笔记

    hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 hibernate的核心类和接口 openSession()和getCurrentSession() 线程局部变量...在web项目中开发hibernate

    无垠式代码生成器最新功能与文档增强版0.7.22

    3)增强修复S2SH技术栈,不再使用openSession语句,而是使用getSessionFactory().getCurrentSession(), Spring 3,Spring 4通用,同时web.xml里增加SpringOpenSessionInViewFilter ===============0.7.19.2==========...

    对DAO编写单元测试[4]

    编写对DAO编写单元测试[4]publicabstractclassTransactionCallback{publicfinalObjectexecute()throwsException{Transactiontx=HibernateUtil.getCurrentSession().beginTransaction();try{Objectr=doInTransaction...

Global site tag (gtag.js) - Google Analytics