Database synchronization and using Jsonobject to let Java Beans "live in full state"

Source: Internet
Author: User

 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
  1. /*
  2. * $filename: Testmain.java,v $
  3. * $Date: 2013-11-28 $
  4. * Copyright (C) Zhenghaibo, Inc. All rights reserved.
  5. * This software are made by Zhenghaibo.
  6. */
  7. Package EDU.NJUPT.ZHB;
  8. Import java.util.ArrayList;
  9. Import java.util.List;
  10. Import Net.sf.json.JSONObject;
  11. /*
  12. * @author: Zhenghaibo
  13. *web:http://blog.csdn.net/nuptboyzhb
  14. *mail: [Email protected]
  15. *2013-11-28 Nanjing,njupt,china
  16. */
  17. Public class Testmain {
  18. /** 
  19. * @param args
  20. */
  21. public static void Main (string[] args) {
  22. //TODO auto-generated method stub
  23. Beanmodel Savebeanmodel = Getinitbean ();
  24. String Clazztype = Savebeanmodel.getclass (). GetName ();
  25. String jsonstring = Jsonobject.fromobject (Savebeanmodel). toString ();
  26. PrintObject (Savebeanmodel); //Print a look first
  27. ////////////////We can regenerate Savebeanmodel objects in the program through Clazztype and Jsonstring
  28. Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
  29. try {
  30. Object object = Jsonobject.tobean (Jsonobject, Class.forName (Clazztype));
  31. /////////////////////object At this time is the one we recovered from the two properties of Clazztype and Jsonstring
  32. PrintObject (object);
  33. } catch (ClassNotFoundException e) {
  34. //TODO auto-generated catch block
  35. E.printstacktrace ();
  36. }
  37. }
  38. public static Beanmodel Getinitbean () {
  39. list<beanmodel> list = new arraylist<beanmodel> ();
  40. For (int i = 0;i<10;i++) {
  41. Beanmodel Beanmodel = new Beanmodel ();
  42. Beanmodel.setdate (new Java.util.Date ());
  43. Beanmodel.setid (i);
  44. Beanmodel.setname ("name" +i);
  45. }
  46. Beanmodel Savebeanmodel = new Beanmodel ();
  47. Savebeanmodel.setid (11);
  48. Savebeanmodel.setdate (new Java.util.Date ());
  49. Savebeanmodel.setname ("Savebean");
  50. Savebeanmodel.setlist (list);
  51. return Savebeanmodel;
  52. }
  53. public static void PrintObject (Object object) {
  54. String result = Jsonobject.fromobject (object). toString ();
  55. System.out.println ("Result:" +result);
  56. }
  57. }

Beanmodel.java

[Java]View Plaincopy
  1. /*
  2. * $filename: Beanmodel.java,v $
  3. * $Date: 2013-11-28 $
  4. * Copyright (C) Zhenghaibo, Inc. All rights reserved.
  5. * This software are made by Zhenghaibo.
  6. */
  7. Package EDU.NJUPT.ZHB;
  8. Import Java.util.Date;
  9. Import java.util.List;
  10. /*
  11. * @author: Zhenghaibo
  12. *web:http://blog.csdn.net/nuptboyzhb
  13. *mail: [Email protected]
  14. *2013-11-28 Nanjing,njupt,china
  15. */
  16. Public class Beanmodel {
  17. private Integer ID;
  18. private Date date;
  19. private String name;
  20. private list<beanmodel> List;
  21. Public Integer getId () {
  22. return ID;
  23. }
  24. public void SetId (Integer id) {
  25. this.id = ID;
  26. }
  27. Public Date getDate () {
  28. return date;
  29. }
  30. public void setDate (date date) {
  31. this.date = date;
  32. }
  33. Public String GetName () {
  34. return name;
  35. }
  36. public void SetName (String name) {
  37. this.name = name;
  38. }
  39. Public list<beanmodel> getList () {
  40. return list;
  41. }
  42. public void setlist (list<beanmodel> List) {
  43. this.list = list;
  44. }
  45. }

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"

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.