JBoss Series 63: The JBoss 7/wildfly cluster Java persistence API (JPA)-II (Hibernate query cache and level two caching sample) __java

Source: Internet
Author: User
Tags dba jboss jboss server wildfly

JBoss JPA hibernate hibernate level two cache Web application

Directory (?) [+] Overview

JBoss Series 56: The JBoss 7/wildfly cluster Java persistence API (JPA)-I discusses the basic JPA-related theories of jboss clustering, and the JPA implementation of JBoss is hibernate, So the JBoss cluster JPA is mainly hibernate cluster, in the basic theory we describe the hibernate cache (query cache) and level two cache, this article gives a hibernate cache (query cache) and a two-level cache example, the example is roughly architecture as shown in the following figure:

As with the two-node cluster, the Web application provides the rest interface for outside access to manipulate the database, and the rest syntax is as follows: http://<ip>:<port>/cluster-demo-jpa/test/addusers/{ Number} is used to add user Http://<ip>:<port>/cluster-demo-jpa/test/getuserbyname/{name} for querying users

The application uses the Jpa/hibnernate operation database, Hibernate level two cache is based on the database. JBoss End and database-side configuration Add Oracle Database driver module to JBoss

Create the Com/oracle/main directory under the jboss_home/modules/directory, copy the Oracle driver Jar (OJDBC6.JAR) to this directory, and create the Module.xml file with this directory, add the following:
[HTML] view plain copy <?xml version= "1.0"  encoding= "UTF-8"?>   < module xmlns= "urn:jboss:module:1.1"  name= "com.oracle" >       < resources>           <resource-root path= " Ojdbc6.jar "/>       </resources>       < dependencies>           <module name= "Javax.api"/ >           <module name= "Javax.transaction.api"/ >       </dependencies>   </module>   Add driver configuration to JBoss server configuration file

Edit the Jboss_home/standalone/configuration/standalone-ha.xml file in <subsystem xmlns= "Urn:jboss:domain: DataSources datasources,drivers Add the following:
[HTML] view plain copy <driver name= Oracle  module= "Com.oracle" >       <xa-datasource-class> oracle.jdbc.xa.client.oraclexadatasource</xa-datasource-class>   </driver>  
Note that this part of the configuration is mentioned in our previous series of JBoss 7/wildfly configuration using the Oracle database.
Oracle Database-side configuration

Connect to the Oracle Database console using the DBA user, create the Demo_user/soong, and assign the appropriate permissions, as follows:
[HTML] view plain copy sqlplus/as sysdba create tablespace testspace datafile '/oracle/oradata/testspace.   DBF ' size 100M create user Demo_user identified by Soong default Tablespace testspace; Grant CONNECT,RESOURCE,DBA to Demo_user;
Start JBoss

With the above configuration complete, we started two JBoss nodes to form a cluster, as follows: [HTML] view plain copy./standalone.sh-c standalone-ha.xml-b Bmanagement= standalone-ha.xml-b 10.66.218 .47-bmanagement=
deployment applied to JBoss

This sample code is located at (HTTPS://GITHUB.COM/KYLINSOONG/CLUSTER/TREE/MASTER/DEMO/JPA), using the method described in Software Installation and data download to download the sample code and compile the code. We need to make some configuration changes before compiling. Configure DataSource

Configure the DataSource file to point to an Oracle database, with the specific edit Jpa/src/main/webapp/web-inf/cluster-jpa-ds.xml file pointing to the Oracle database: [HTML] View plain copy <datasource jndi-name= "Java:jboss/datasources/jbossclusterdemojpads"       pool-name= "Tasks-rs-xml-quickstart"  enabled= "true"        Use-java-context= "true" >      <connection-url>jdbc:oracle:thin:@//</connection-url>      <driver>oracle</driver >      <security>         < user-name>demo_user</user-name>         <password>soong </password>      </security>   </datasource>  
Configure Persistence-unit

Configuration Persistence-unit, including datasource related, cache equivalent, specific edit jpa/src/main/resources/meta-inf/persistence.xml, configuration content as follows:
[HTML] View Plain copy <persistence-unit name= "PRIMARY" >      <jta-data-source> java:jboss/datasources/jbossclusterdemojpads</jta-data-source>      < shared-cache-mode>enable_selective</shared-cache-mode>      <properties >         <!-- properties for hibernate -- >         <property name= "Hibernate.dialect"  value= " Org.hibernate.dialect.Oracle10gDialect "/>         <property  Name= "Hibernate.default_schema"  value= "Demo_user"/>          <!-- use  ' create '  or  ' Create-drop '  first time -->          <property name= "Hibernate.hbm2ddl.auto"  value= "Validate"  />     &Nbsp;    <property name= "Hibernate.show_sql"  value= "false"  />          <property name= "Hibernate.cache.use_second_level_cache"  value= "true"/>     <property name= "Hibernate.cache.use_query_cache"   Value= "true"  />        </properties>   </ persistence-unit>  
Compiling build deployment Packages

Using the methods described in Software installation and data downloading, perform
[HTML] view plain copy mvn clean install
deployment applications and testing

The completion of the steps above will generate the Jpa/target/cluster-demo-jpa.war file and deploy the application using the 4-way deployment application to the Jboss7/wildfly description method. Test Add User

Add users to Node 1 after successful deployment:


After adding success, the page returns a string that resembles: "Add 1 users to database spend 154 milliseconds" Test query user


Querying user name-499 through Node 2 will return a string similar to: "Get 164 users spend 614 milliseconds" understanding invalidation

In this example, the cluster two nodes are invalidation (invalidated) to ensure data continuity, and the failure mode is a cluster cache, but in fact the nodes in the cluster do not share any data, but only from the running node in the removal may be outdated data, detailed understanding of the reference JBoss Data Grid (Infinispan) caching mode.

Related Article

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.