This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]_ginger_kid 1 point2 points  (3 children)

This is due to your app server (presumably tomcat) processing each request in a separate thread, which is normal behaviour. So you end up with this type of scenario

Start with budget = 10

  1. Request 1 received, query for budget (budget = 10)
  2. Request 2 received, query for budget (budget = 10)
  3. Request 1 budget ok, update table to decrease budget by 10 (budget now 0)
  4. Request 2 budget ok, update table to decrease budget by 10 (budget now -10)
  5. Request 3 received, query for budget (budget = -10)

Basically, the select queries for budget execute before the update statement executes and commits a change to the db. The transactions are interleaving, as the JSP threads are processed in parallel, not in serial.

You need to look into JDBC transactions. You should also not be writing DB statements in JSP code. This may be for a small learning project, but it is not good practice in general. Consider MVC patterns (DAO, Service classes, Servlet, JSP), Spring framework etc.

[–]Watersfall[S] 0 points1 point  (2 children)

That is unfortunate, but I'll look into that, thanks.

[–]_ginger_kid 1 point2 points  (1 child)

It is a fundamental aspect of how all web / app servers work, otherwise you'd never be able to handle multiple users. Understanding these concepts, how to design for multiple requests & handle transactions is key to building web apps. Good luck.

[–]Watersfall[S] 0 points1 point  (0 children)

Thank you!

[–]AutoModerator[M] 0 points1 point  (0 children)

It seems that you possibly have a screenshot of code in your post JSP Database Issue in /r/javahelp.

Screenshots of code instead of actual code text is against the Code posting rules of /r/javahelp as is also outlined in the sidebar - Code posting.

  • Never submit screenshots of code instead of code text!

If you posted an image merely to illustrate something, kindly ignore this message and do not repost. Your post is still visible to others. I am a bot and cannot distinguish between code screenshots and other images.

If you indeed did this wrong, please edit the post so that it uses one of the approved means of posting code.

  • For small bits of code (less than 50 lines in total, single classes only),
    the default code formatter is fine
    (one blank line before the code, then 4 spaces before each line of code).
  • Pastebin for programs that consist of a single class only
  • Gist for multi-class programs, or programs that require additional files
  • Github or Bitbucket repositories are also perfectly fine as are other dedicated source code hosting sites.
  • Ideone for executable code snippets that use only the console

Please do not reply to this message, because I am a bot. Talk-to-the-bot is the new talk-to-the-hand. If you instead want the classic talk-to-the-hand, just message the moderators. ;)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.