0
votes

i have a method in backing bean that adds a product in to db. it checks if product is duplicate. however, it's a slow process and JSF UI could fire multiple requests at same time, and it fails to check duplicate when adding stuff in db due to multiple thread run this application at same time. what's best way to make this method thread safe. make it as a synchronized method?

public int addProduct(final ActionEvent event){
// check duplicates code....long process
// insert product in db code....long process
}
1

1 Answers

2
votes

Do not make it synchronized in the following cases - it will either have no effect (request-scoped bean) or will kill your application (application-scoped bean). For a session-scoped bean it won't have significant side-effects.

But usually the way to counter this is to disable the UI component that fires the request.