Hibernate二級緩衝配置

來源:互聯網
上載者:User

標籤:

實體類

 1 package cn.happy.entity; 2  3 public class Emp { 4     private Integer empNo;      5     private String empName; 6     public Integer getEmpNo() { 7         return empNo; 8     } 9     public void setEmpNo(Integer empNo) {10         this.empNo = empNo;11     }12     public String getEmpName() {13         return empName;14     }15     public void setEmpName(String empName) {16         this.empName = empName;17     }18     19     20 21 }

工具類

 1 package cn.happy.util; 2  3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6  7 public class HibernateUtil { 8     private static final ThreadLocal sessionTL=new ThreadLocal(); 9     private static Configuration cf;10     private static final SessionFactory factory;11     static{12         try {13             cf=new Configuration().configure();14             factory=cf.buildSessionFactory();15         } catch (Exception e) {16             throw new ExceptionInInitializerError(e);17         }18     }19     public static Session getSession()20     {    21         //sessionTL的get()方法根據當前線程返回其對應的線程內部變數,22                 //也就是我們需要的Session,多線程情況下共用資料庫串連是不安全的。23                 //ThreadLocal保證了每個線程都有自己的Session。24         Session session = (Session)sessionTL.get();25         //如果session為null,則開啟一個新的session26         if (session==null) {27             //建立一個資料庫連接對象session28             session=factory.openSession();29             //儲存該資料庫連接session到ThreadLocal中。30             sessionTL.set(session);31             32         }33         //如果當前線程已經訪問過資料庫了,34                 //則從sessionTL中get()就可以擷取該線程上次擷取過的資料庫連接對象。35                 return session; 36     }37     /**38      * 關閉Session39      */40     public static void closeSession()41     {42         Session session =(Session)sessionTL.get();43         sessionTL.set(null);44         session.close();45     }46 47 }

測試類別

package cn.happy.test;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import cn.happy.entity.Emp;import cn.happy.util.HibernateUtil;public class STest {    Transaction tx;    Session session;    Transaction tx2;    Session session2;     @Test       public void testBulk(){            session = HibernateUtil.getSession();            tx=session.beginTransaction();           Emp emp = (Emp)session.get(Emp.class, 1);           System.out.println(emp);           tx.commit();           HibernateUtil.closeSession();           System.out.println("===================");           session2 = HibernateUtil.getSession();            tx2=session2.beginTransaction();           Emp emp2 = (Emp)session2.get(Emp.class, 1);           System.out.println(emp2);           tx2.commit();           HibernateUtil.closeSession();       }    }

小配置

<?xml version=‘1.0‘ encoding=‘utf-8‘?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">    <hibernate-mapping package="cn.happy.entity">     <class name="Emp" table="Emp">     <cache usage="read-write"/>         <id name="empNo" type="int" column="EMPNO">         <generator class="native">         </generator>        </id>         <property name="empName" type="string" column="EMPNAME"/>     </class>     </hibernate-mapping>

大配置

<?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">    <hibernate-configuration>    <session-factory>    <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>    <property name="connection.username">zc</property>        <property name="connection.password">zc</property>        <!-- 輸出所有 SQL 陳述式到控制台。 -->        <property name="hibernate.show_sql">true</property>        <!-- 配置Hibernate.cfg.xml開啟二級緩衝。 -->          <property name="hibernate.cache.use_second_level_cache">true</property>          <!-- 配置二級緩衝的供應商 -->        <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>                <!-- 在 log 和 console 中列印出更漂亮的 SQL。 -->        <property name="hibernate.format_sql">true</property>        <!-- 自動產生資料表,update/create -->        <property name="hbm2ddl.auto">update</property>        <!-- 方言 -->        <property name="hibernate.dialect">    org.hibernate.dialect.Oracle10gDialect</property>        <!-- 關聯小配置 -->                <mapping resource="cn/happy/entity/Emp.hbm.xml"/>        <class-cache    usage="read-write" class="cn.happy.entity.Emp"/>    </session-factory>    </hibernate-configuration>

Jar包匯入

package cn.happy.entity;
public class Emp {private Integer empNo; private String empName;public Integer getEmpNo() {return empNo;}public void setEmpNo(Integer empNo) {this.empNo = empNo;}public String getEmpName() {return empName;}public void setEmpName(String empName) {this.empName = empName;}
}

 

Hibernate二級緩衝配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.