Send database data through socket connection

可紊 提交于 2020-01-04 06:05:22

问题


I have a distributed java application and I want to send the database data between two databases which are in seperate computer systems or android devices. Which is the best way to transfer tha table data throw a socket connection?I think about send table rows as a text lines, or xml elements or create a class(implements the Serializable inteface) for every table and tranfer an arraylist of that objects for each table. Which is in your opinion the more effetive solution? There is any other way to do that? the databases may are different like mysql, sqlite, h2database so I can't use any database native function to syncronize(here while I serach for somthing similar I read something about orable database, have a function like this).

Thank you very much!!


回答1:


Personally I consider XML too verbose for transferring data, you might end up with half the amount of data for content and half for structure.

<record>
   <name>John</name>
   <age>30</age>
</record>

Most of the space is lost in defining the structure, little is left for the data you want (John, 30).

Plain text lines are too limited, at least consider comma seperated values if you must.

Name;Age
John;30

The first row is just for labels, if you're pretty sure that will never change you can remove it.

Serialization of objects can be too dangerous to transfer data, since you might create incompatibilities over time that make deserialization fail. For example, introducing or removing a field is enough.

Have a look at JSON, it's not as verbose as XML and more structured than just lines. The important part is of course that you handle your data properly at both ends, but given you know the structure sent you can transform it properly.

Example:

{name: John, age: 30 }



回答2:


Just some ideas for the steps i would take if i was doing this.

First read the data from the databases into some common format on each machine or device. Using JPA to do would be easiest.

Because the two devices need to be in sync you need a common spot to compare. Then, if there are discrepancies in the data you need to decide which copy is correct. Or, if this is a single table, simply take a union of both copies and then push that back to each machine.

So, i would use a queuing server to do this. On each machine, read the data using JPA and post it using Gson to the queuing server (for which i would use HornetQ). The data from both machines arrives on an input queue.

Then have one reader for this input queue which munges the data together discarding duplicates and coming up with the canonical view of the data. Finally, this data is then pushed (again with Gson) to a JMS topic to which both (or all) devices are subscribed.

Then each device gets each row of the database and can add or update accordingly. You would need some special log on each device to record deletes if you ever wanted to remove data everywhere as well.

That's just a starter...! :)



来源:https://stackoverflow.com/questions/5808256/send-database-data-through-socket-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!