开发者

database sharing between two servers

开发者 https://www.devze.com 2022-12-13 16:37 出处:网络
My current set u开发者_如何转开发p is a single dedicated server with Java, hibernate app running on tomcat, apache http server, MYSQL.

My current set u开发者_如何转开发p is a single dedicated server with Java, hibernate app running on tomcat, apache http server, MYSQL.

I need to get a second server to share the load, but using the same database from the first server.

The backend processing(excluding db transaction) is time consuming, hence the second server for backend processing).

Will there be any unwanted consequences of this setup? Is this the optimal setup?

My apps do update/delete and has transaction control as follows:

beginTransaction();
            getSession().save(obj);
            //sessionFactory.openSession().save(obj);
            commitTransaction()


As long as only one of the apps does database updates on a shared table you should be fine. What you definitely don't want to happen is:

app1: delete/update table1.record24
app2: delete/update table1.record24

because when Hibernate writes the records one of the processes will notice the data has changed and throw an error. And as a classic Heisenbug it's really difficult to reproduce.

When, on the other hand, the responsibilities are clearly separated (the apps share data for reading, but do not delete/update the same tables) it should be ok. Document that behavior though as a future upgrade may not take that into account.

EDIT 1:Answering comments

You overcome concurrency issues by design. For any given table:

  • Both apps may insert
  • Both apps may select
  • one of the apps may also update / delete in that table

Your frontend will probably insert into tables, and the backend can read those tables, update rows where necessary, create new result rows, and delete rows as cleanup.

Alternatively, when the apps communicate, the frontend can transfer ownership of the records for a given task to the business backend, which gives the ownership back when finished. Make sure the hibernate cache is flushed (transaction is executed) and no hibernate objects of that task are in use before transferring ownership.

The trick of the game is to ensure that Hibernate will not attempt write records which are changed by the other app, as that will result in a StaleStateException.

And example of how I solved a similar problem:

  • app 1 receives data, and writes it in table1
  • app 2 reads table1, processes it, and writes/updates table2
  • app 2 deletes the processed records in table1

Note that app 1 only writes to the shared table. It also reads, writes and updates from other tables, but those tables are not accessed by app 2, so that's no problem.


It is a fairly common approach, both for failover and load balancing.

Here's a short article describing the setup: http://raibledesigns.com/tomcat/

Beware of singletons in this setup.

0

精彩评论

暂无评论...
验证码 换一张
取 消