Category: "java"2013-11-28 21:04 729 people read reviews (0) favorite reports
Introduction
Why do I write such a simple question? First introduce the project background. A recent need to do a database synchronization work, that is, a Web application has two databases, one is its own local database, and the other is a remote database. When we access the Web project, the local database to add and delete, but also to the remote database to be modified. Our idea is that we first inject two DAO objects into the spring configuration file, and then when the service layer makes additions and deletions, we call the DAO of the local database in Basedao and call the remote DAO.
Key issues:
When the local database additions and deletions are successful, and the remote database and additions and deletions are unsuccessful, we need to treat them with exception. We need to save the modified object and then insert it into the local synchronization table, when the remote database is accessible, then the data in the local synchronization table is taken out, and then generate the corresponding object object, and then use the remote database DAO for additions and deletions, Delete the synchronization table record of the local database after adding and removing.
Prerequisites:
1. The system has been developed before, there is no database synchronization function
2. System development using SSH framework
3. All the additions and deletions of the system are implemented by the Hibernate method with the service layer calling the DAO layer.
Questions:
1. Why not modify each service layer at the time of synchronization?
For a relatively large system, the service layer has many classes, if a service a service modification, work too large, easy to change the wrong
2. Why don't you add a field to the database to mark the data for synchronization?
The system has dozens of database tables, so if you do, the code changes too much
3. How to design the synchronization table, how to synchronize the contents of the synchronization table?
Design of synchronization table
CREATE TABLE Synctbl (
ID VARCHAR2 (+) primary key,
Type VARCHAR2 (TEN) is not NULL,
ClassName VARCHAR2 () not NULL,
Syncstring VARCHAR2 (+) NOT NULL,
Createtime Date
);
ID; primary key, UUID can be
Type: operation (increment, delete, change, or HQL statement)
ClassName: The class name of the Hibernate entity class for the database table
Syncstring: The serialized string for the object to be added or deleted, or the HQL statement
Createtime: The time the record was generated
Text:
The above logic is a bit complicated and difficult to describe clearly in short text. I'll use Jsonobject to let Hibernate's entity classes generate strings, and then restore that entity class based on the names of these strings and entity classes.
The following jar packages are required to use Jsonobject.
1.commons-beanutils-1.7.0.jar
2.commons-collections-3.2.jar
3.commons-lang-2.3.jar
4.commons-logging-1.0.4.jar
5.ezmorph-1.0.6.jar
6.json-lib-2.1.jar
Then we create a Java project to demonstrate:
Project Source code: Https://github.com/nuptboyzhb/JavaJsonObjectBean
[Java]View Plaincopy
- /*
- * $filename: Testmain.java,v $
- * $Date: 2013-11-28 $
- * Copyright (C) Zhenghaibo, Inc. All rights reserved.
- * This software are made by Zhenghaibo.
- */
- Package EDU.NJUPT.ZHB;
- Import java.util.ArrayList;
- Import java.util.List;
- Import Net.sf.json.JSONObject;
- /*
- * @author: Zhenghaibo
- *web:http://blog.csdn.net/nuptboyzhb
- *mail: [Email protected]
- *2013-11-28 Nanjing,njupt,china
- */
- Public class Testmain {
- /**
- * @param args
- */
- public static void Main (string[] args) {
- //TODO auto-generated method stub
- Beanmodel Savebeanmodel = Getinitbean ();
- String Clazztype = Savebeanmodel.getclass (). GetName ();
- String jsonstring = Jsonobject.fromobject (Savebeanmodel). toString ();
- PrintObject (Savebeanmodel); //Print a look first
- ////////////////We can regenerate Savebeanmodel objects in the program through Clazztype and Jsonstring
- Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
- try {
- Object object = Jsonobject.tobean (Jsonobject, Class.forName (Clazztype));
- /////////////////////object At this time is the one we recovered from the two properties of Clazztype and Jsonstring
- PrintObject (object);
- } catch (ClassNotFoundException e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- public static Beanmodel Getinitbean () {
- list<beanmodel> list = new arraylist<beanmodel> ();
- For (int i = 0;i<10;i++) {
- Beanmodel Beanmodel = new Beanmodel ();
- Beanmodel.setdate (new Java.util.Date ());
- Beanmodel.setid (i);
- Beanmodel.setname ("name" +i);
- }
- Beanmodel Savebeanmodel = new Beanmodel ();
- Savebeanmodel.setid (11);
- Savebeanmodel.setdate (new Java.util.Date ());
- Savebeanmodel.setname ("Savebean");
- Savebeanmodel.setlist (list);
- return Savebeanmodel;
- }
- public static void PrintObject (Object object) {
- String result = Jsonobject.fromobject (object). toString ();
- System.out.println ("Result:" +result);
- }
- }
Beanmodel.java
[Java]View Plaincopy
- /*
- * $filename: Beanmodel.java,v $
- * $Date: 2013-11-28 $
- * Copyright (C) Zhenghaibo, Inc. All rights reserved.
- * This software are made by Zhenghaibo.
- */
- Package EDU.NJUPT.ZHB;
- Import Java.util.Date;
- Import java.util.List;
- /*
- * @author: Zhenghaibo
- *web:http://blog.csdn.net/nuptboyzhb
- *mail: [Email protected]
- *2013-11-28 Nanjing,njupt,china
- */
- Public class Beanmodel {
- private Integer ID;
- private Date date;
- private String name;
- private list<beanmodel> List;
- Public Integer getId () {
- return ID;
- }
- public void SetId (Integer id) {
- this.id = ID;
- }
- Public Date getDate () {
- return date;
- }
- public void setDate (date date) {
- this.date = date;
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- Public list<beanmodel> getList () {
- return list;
- }
- public void setlist (list<beanmodel> List) {
- this.list = list;
- }
- }
Note: For our "Recover" object, we can use Hibernate's getcurrentsession to save the operation. However, if you use Getcurrentsession to update or delete this object, hibernate will error. At this point, we need to use Hibernate's Sessionfactory.opensession method to get the session and then perform the update or delete operation.
Not permitted for commercial purposes without permission
Database synchronization and using Jsonobject to let Java Beans "live in full state"