简体   繁体   English

如何保护数据库免受两个客户端在Web应用程序中写入数据库的影响

[英]how to protect a database from two clients writing to it in web application

Hi I have a question about multithreading in web app. 嗨,我对网络应用中的多线程有疑问。 In my app I have a Controller which passes parameters to a Business Logic class and it talks to the database, basically It loads the data to db and then make some query and receive the answer. 在我的应用程序中,我有一个Controller,该Controller将参数传递给Business Logic类,并与数据库进行对话,基本上它将数据加载到db,然后进行查询并接收答案。 So, I wanted to make those methods synchronised so that only one client could load the data to the database but now I am really confused because normally every request fires new thread(I think from what I have read) and I am afraid that for eg. 因此,我想使这些方法同步,以便只有一个客户端可以将数据加载到数据库中,但是现在我真的很困惑,因为通常每个请求都会触发一个新线程(我认为从我读过的内容来看),而且我担心例如。 Lets say I have two clients and client1 works with thread 1 and he accesses the new thread that is created with the help of Runnable Interface with synchronised method and client 2 works with thread 2 I am afraid that he can also access synchronised method because he has got his own thread. 可以说我有两个客户端,client1使用线程1,他访问通过Runnable Interface帮助使用同步方法创建的新线程,而client 2使用线程2使用,恐怕他也可以访问同步方法,因为他拥有有自己的线程。 That has nothing to do with thread1. 这与thread1无关。 Is it possible that 2 clients will be able to access those synchronised methods of their own? 2个客户端是否可能能够访问自己的那些同步方法? Or should I destroy new Thread that I am creating and make all classes synchronised. 还是应该销毁我正在创建的新线程并使所有类同步。 So I am so confused how to protect a database from two clients writing to it? 因此,我很困惑如何保护数据库免受两个客户端写入数据库的困扰? Thanks in advance. 提前致谢。

Apache ActiveMQ can solve your problem. Apache ActiveMQ可以解决您的问题。

What you will be doing is configure/install Apache ActiveMQ on your webserver. 您将要做的是在Web服务器上配置/安装Apache ActiveMQ。 In that you can create a queue (say DBrequests) which will be responsible for holding your queries to be posted to database. 这样,您可以创建一个队列(例如DBrequests),该队列将负责保存要发布到数据库的查询。

There will be Consumers and Producers. 将有消费者和生产者。 In your case client1 and client2 are producers (ie it produces requests to be posted to databases). 在您的情况下,client1和client2是生产者(即,它产生要发布到数据库的请求)。 Your thread will put the requests into Queue. 您的线程会将请求放入队列。

Now your consumer will come into effect. 现在,您的消费者将生效。 Consumer thread will always be listening to queue if there is any message. 如果有任何消息,使用者线程将始终在侦听队列。 Once the message is available you can use that message to fire a database query. 消息可用后,您可以使用该消息来触发数据库查询。

There is a hello world example here: http://activemq.apache.org/hello-world.html 这里有一个hello world示例: http : //activemq.apache.org/hello-world.html

You can do something like this. 你可以做这样的事情。 Using the parameters from controller build a query in HelloWorldProducer and fire that query in HelloWorldConsumer. 使用控制器中的参数在HelloWorldProducer中构建查询,并在HelloWorldConsumer中触发该查询。 Your problem should be solved. 您的问题应该得到解决。

Most databases synchronize requests, so as long as you put database statements in the same transaction if they need to deal with a self consistent database view, and as long as the database isolation level is set appropriately, you don't need to worry about thread synchronization. 大多数数据库都会同步请求,因此,只要您需要将数据库语句放在同一事务中,如果它们需要处理自洽的数据库视图,并且只要适当地设置了数据库隔离级别,就不必担心线程同步。 The exception would be if you use SQLite, which doesn't really support multithreading. 例外是如果您使用SQLite,它实际上并不支持多线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM