私は、Javaのメソッドは、Webサービスに呼び出し、応答に基づいてデータベースに変更を加えています。私の仕事は、複数のユーザーが同時にこのアプリケーションを使用するときに並行性エラーを排除することです。のJavaのWebサービスの同時実行の問題
私は一日中ロックデータベースの様々なタイプを使用しようとしていたが、何も働きました。私は最終的にプロセス要求メソッドでsynchronizedを使用しようとしました。
私の全体のアプリケーションはシングルスレッドです。同期がなぜこれを解決するのですか?
編集:コードを追加しました。
public class ProcessMakePaymentServlet extends HttpServlet {
private DbBean db = new DbBean();
protected synchronized void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// defining variables...
try {
// initialize parameters for invoking remote method
db.connect();
startTransaction(); //autocommit=0; START TRANSACTION;
// process debit
//this method gets the method using a select...for update.
//it then updates it with the new value
successfulDebit = debitAccount(userId, amt);
if (successfulDebit) {
// contact payment gateway by invoking remote pay web service method here.
// create new instances of remote Service objects
org.tempuri.Service service = new org.tempuri.Service();
org.tempuri.ServiceSoap port = service.getServiceSoap();
// invoke the remote method by calling port.pay().
// port.pay() may time out if the remote service is down and throw an exception
successfullyInformedPaymentGateway = port.pay(bankId, bankPwd, payeeId, referenceId, amt);
if (successfullyInformedPaymentGateway) {
// insert payment record
recordPaymentMade(userId, amt, referenceId);
//call to the database to record the transaction. Simple update statement.
out.println("<br/>-----<br/>");
//getTotalPaymentMade does a select to sum all the payment amounts
out.println("Total payment made so far to gateway: " + getTotalPaymentMade());
commitTransaction();// calls COMMIT
db.close(); //connection closed.
} else {
rollbackTransaction();//calls ROLLBACK
db.close();
successfulDebit = false;
out.println("<br/>-----<br/>");
out.println("Incorrect bank details.");
}
} else {
rollbackTransaction();//calls ROLLBACK
db.close();
out.println("<br/>-----<br/>");
out.println("Invalid payment amount.");
}
} catch (Exception ex) {
try {
rollbackTransaction();//calls ROLLBACK
db.close();
} catch (Exception ex1) {
}
}
}
だから、あなたのWebサービスへの同時アクセスは、1つのクライアントだけが存在しませんか? – home
「アプリケーション全体がシングルスレッドである」とはどういう意味ですか? –
@Dave Newton私のクラスのどれも、スレッドを拡張するものはありません。 – nknj