两台服务器之间的数据库共享

两台服务器之间的数据库共享

本文介绍了两台服务器之间的数据库共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的设置是一个单独的专用服务器与Java,hibernate应用程序运行在tomcat,apache http服务器,MYSQL。



我需要获得第二个服务器共享负载,但使用与第一个服务器相同的数据库。



后端处理(不包括db事务)是耗时的,因此后端处理的第二个服务器)。



此设置是否会有不必要的后果?这是最佳设置吗?



我的应用程序更新/删除并具有如下的交易控制:

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


解决方案

只要有一个应用程序在共享表上你应该很好。你绝对不想发生的是:

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

因为当Hibernate写入记录时,一个进程会注意到数据已更改并抛出一个错误。



另一方面,当责任明确分开时(应用程序共享数据进行阅读,但不删除) /更新相同的表)它应该是确定。



编辑1:回答评论

您通过设计克服了并发问题。对任何给定的表格:




  • 两个应用程式都可以插入


  • 其中一个应用程式也可能会在该表格中更新/删除



您的前端可能会插入表格,并且后端可以读取这些表,
在必要时更新行,创建新的结果行,并将行删除为清除。



前端可以将给定任务的记录的所有权转移到业务后端,从而在完成后返回所有权。确保hibernate缓存刷新(事务被执行),并且在转移所有权之前没有任务的任何hibernate对象正在使用。



游戏的窍门是确保Hibernate不会尝试写入由其他应用程序更改的记录,因为这将导致StaleStateException。



我解决了类似问题的例子:




  • app 1接收数据, it in table1

  • app 2读取table1,处理和写入/更新table2

  • app 2删除table1中处理的记录



请注意,应用程序1只写入共享表。它也从其他表读取,写入和更新,但这些表不能由应用程序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()
解决方案

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.

这篇关于两台服务器之间的数据库共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:24