Hibernate level Two cache configuration

Source: Internet
Author: User

Entity class

1  packagecn.happy.entity;2 3  public classEMP {4     PrivateInteger empNo; 5     PrivateString empname;6      publicInteger getempno () {7         returnempNo;8     }9      public voidsetempno (Integer EmpNo) {Ten          this. EmpNo =empNo; one     } a      publicString getempname () { -         returnempname; -     } the      public voidsetempname (String Empname) { -          this. EmpName =empname; -     } -      +      -  +}

Tool class

1  packagecn.happy.util;2 3 Importorg.hibernate.Session;4 Importorg.hibernate.SessionFactory;5 Importorg.hibernate.cfg.Configuration;6 7  public classHibernateutil {8     Private Static FinalThreadLocal sessiontl=NewThreadLocal ();9     Private StaticConfiguration cf;Ten     Private Static Finalsessionfactory factory; one     Static{ a         Try { -cf=NewConfiguration (). Configure (); -factory=cf.buildsessionfactory (); the}Catch(Exception E) { -             Throw NewExceptionininitializererror (e); -         } -     } +      public StaticSession getsession () -     {     +         //the get () method of the Sessiontl returns its corresponding thread internal variable according to the current thread . a                 //that is the session we need, the shared database connection is not secure in multi-threaded situations.  at                 //threadlocal ensures that each thread has its own session.  -Session session =(Session) sessiontl.get (); -         //if session is null, a new session is opened -         if(session==NULL) { -             //Create a database connection object session -session=factory.opensession (); in             //Save the database connection session to Threadlocal.  - Sessiontl.set (session); to              +         } -         //if the current thread has accessed the database, the                 //a get () from Sessiontl can get the database connection object that the thread last Fetched.  *                 returnsession; $     }Panax Notoginseng     /** - * Close Session the      */ +      public Static voidclosesession () a     { theSession session =(Session) sessiontl.get (); +Sessiontl.set (NULL); - session.close (); $     } $  -}

Test class

 packagecn.happy.test;Importorg.hibernate.Session;Importorg.hibernate.Transaction;Importorg.junit.Test;Importcn.happy.entity.Emp;Importcn.happy.util.HibernateUtil; public classstest {Transaction tx;    Session session;    Transaction tx2;     Session session2; @Test public voidtestbulk () {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 (); }    }

Small configuration

<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-     mappingpublic "-//hibernate/hibernate mapping DTD 3.0//en"    "/http WWW.HIBERNATE.ORG/DTD/HIBERNATE-MAPPING-3.0.DTD "> Package    =" cn.happy.entity ">     <  Class name= "emp" table= "emp" >     <cache usage= "read-write"/> <id name=         "empNo" type= "int" column= "EMPNO" >         class= "native" >         </generator>        </id>         < Property name= "empname" type= "string" column= "empname"/>     </class>     </ Hibernate-mapping>

Large configuration

<?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>    < propertyname= "connection.driver_class">Oracle.jdbc.OracleDriver</ property>    < propertyname= "connection.url">Jdbc:oracle:thin: @localhost: 1521:ORCL</ property>    < propertyname= "connection.username">Zc</ property>        < propertyname= "connection.password">Zc</ property>        <!--output All SQL statements to the Console.  -        < propertyname= "hibernate.show_sql">True</ property>        <!--Configure Hibernate.cfg.xml to turn on level two caching.  -          < propertyname= "hibernate.cache.use_second_level_cache">True</ property>          <!--Configure a vendor for level two cache -        < propertyname= "hibernate.cache.provider_class">Org.hibernate.cache.EhCacheProvider</ property>                <!--print more beautiful SQL in log and Console.  -        < propertyname= "hibernate.format_sql">True</ property>        <!--automatically generate data tables, update/create -        < propertyname= "hbm2ddl.auto">Update</ property>        <!--dialect -        < propertyname= "hibernate.dialect">Org.hibernate.dialect.Oracle10gDialect</ property>        <!--Associate Small Configuration -                <MappingResource= "cn/happy/entity/emp.hbm.xml"/>        <Class-cacheusage= "read-write"class= "cn.happy.entity.Emp"/>    </session-factory>    </hibernate-configuration>

Jar Package Import

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 level Two cache configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.