• hibernate---核心开发接口1(重点)


    面试考这个比较少

    a) Session session = sessionFactory.openSession();    永远都是打开新的 记得要 close


    b)  Session session = sessionFactory.getCurrentSession();  

    1.先找原来是否有存在,有就用 没有就开一个(在未提交之前永远都是一样的)用途:

    2.界定事务边界事务提交自动close 

    3.current_session_context_class(jta,thread)

      thread使用 connection

     <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>

    一般用thread 有session就用当前的session   jta等学了EJB3.0 用于分布式(不止一个数据库)

    还有如果设为 
    <property name="current_session_context_class">jta</property>

    是不行的,application提供,tomcat本身不具备这样的能力(jboss可以,或者用spring也可以),

    那要是
    <property name="current_session_context_class">thread</property>
    这行去掉,

    如果要用getCurrentSession是不行了,因为我们没有设置session的上下文,既不是thread也不是jta,所以会报错


    11.1. Session和事务范围(transaction scope)

    11.2.1. 非托管环境

    11.2.2. 使用JTA(学了EJB3再说)




    		//Session session = sessionFactory.openSession();
    		Session session = sessionFactory.getCurrentSession();
    		
    		session.beginTransaction();
    		session.save(t);
    		
    		Session session2 = <span style="font-family: Arial, Helvetica, sans-serif;">sessionFactory.getCurrentSession();</span>
    
    
    		
    		System.out.println(session == session2) //输出;true
    		
    		session.getTransaction().commit();   //session 提交后 既session2跟着提交了
    		
    		Session session3 = sessionFactory.getCurrentSession();//重新打开一个
    		
    		System.out.println(session == session3);//输出:false





    版权声明:本文为博主原创文章,未经博主允许不得转载。

    today lazy . tomorrow die .
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    数据库周刊第十七期来啦!
    一文详解MySQL中的事件调度器EVENT
    新买的Mac该如何保养?
    五步搭建自助BI平台,业务分析自给自足,不再等待!
    Oracle坏块处理常用SQL
    懒人看执行计划神器 for Oracle
  • 原文地址:https://www.cnblogs.com/france/p/4808667.html
Copyright © 2020-2023  润新知