同步Web服务方法是否有意义? [英] Does it make sense to synchronize web-service method?

查看:109
本文介绍了同步Web服务方法是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用Java编写并托管在JBoss AS上的Web服务.我不是Web服务设计专家,但是我能正确获得它吗?每次调用该服务都会启动一个新的 thread 而不是一个新的流程?在我的服务中使用同步方法是否有意义?我需要有一种方法,一次只能为一个用户调用一次,而不能同时为多个用户调用.

I'm creating a web-service written in Java and hosted on JBoss AS. I'm not a professional in web-service design yet but do I get it correctly and each call to the service initiates a new thread and not a new process? Does it make sense to have synchronized methods in my service? I need to have a method which is invoked only for one user at a time not simultaneously for multiple.

推荐答案

是的,请求由各个处理程序线程处理.所有JBoss都有一个过程.

Yes, requests are handled by individual handler threads. There is a single process for all of JBoss.

如果您的应用程序最终跨群集中的多个节点托管,则同步可能会出现问题.如果没有Terracotta之类的魔术的帮助,锁将不会在多个JVM中传播.对于一个简单的解决方案,您可以在数据库中使用悲观的行锁来控制访问.当然,人们倾向于挑战需要阻塞方法的整个设计,并寻找可以并行运行的替代方案.

Synchronization can be problematic if your application ends up getting hosted across multiple nodes in a cluster. The locks won't propagate across multiple JVMs without the help of some magic like Terracotta. For a simple solution you can use a pessimistic row lock in your database to control access. One would of course be inclined to challenge the entire design that requires a blocking method and look for an alternative that can run in parallel.

此外,如果您要使用那条路线,则java.util.concurrent包中的锁优先于synced关键字.

Also, Locks from the java.util.concurrent package are preferred to the synchronized keyword if you are going that route.

这篇关于同步Web服务方法是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆