views:

90

answers:

2

My current set up 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()
A: 

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.

crowne
A: 

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.

extraneon
Both my apps do update/delete, but I do have transaction control(see edit above), would it still have issue like you mentioned?
how do I overcome that issue then?
transaction control does not work as app 2 does not know that app 1 starts a transaction. The database does know, but databases don't push information. You might work with pessimistic locking though, that's a bit like synchronized for databases:) That will limit performance but make the system more robust. I however prefer solving my problems by design.
extraneon
What I have here are two apps with identical codebase that both do update/delete/read on the same database. I created the second app on a second server to spread the load. Is pesismistic locking the only option left then?
I realize that what I should do is: when hibernate throws an error because the underlying data has changed, I should resolve the difference right there(either refetch the new value, then apply the change; or ignore it).
It is harder to think out what might be an appropriate action than to design it right in the first place. If someone is manually editting the data (mysql command prompt or so), than there's nto much you can do. But otherwise, how do you know whether data changed should overwrite new data, o rjust be forgotten?
extraneon
By the way, I'm talking about TABLES, not databases. You can have as many updaters in a database as you like, as long as they do not try to edit the same records at the same time.
extraneon