3

If I have following sequence in a function

void UpdateDatabase(conn) { createStatement executeStaement getResult } 

Is this sequence of calls multithreading safe in Java

4
  • 1
    I think you need to show us some more code snippet, like what parameters are used by createStatement, executeStaement and getResult. How are you calling the UpdateDatabase function Commented Apr 17, 2013 at 8:40
  • Also it will depend on what database you are using as well. Typically databases will manage concurrent requests. So you wouldn't worry about thread safety at db client side. Commented Apr 17, 2013 at 8:45
  • possible duplicate of Is java.sql.Connection thread safe? Commented Apr 17, 2013 at 8:50
  • 1
    Just make sure every thread uses its own Connection. Commented Apr 17, 2013 at 8:56

2 Answers 2

2

Asuming your threads don't share any state or otherwise synchronize the shared state correctly the execution is only thread safe when viewing what happens inside of the JVM. More importantly however is if your data can still be corrupted.

Every JDBC connection should only be used by one thread at a time, which you are doing. Database systems however define four isolation levels, defining which state of the data concurrent transactions can see. If your concurrent transactions don't touch the same data your fine. If they do, have a look at the isolation level of your database.

Sign up to request clarification or add additional context in comments.

Comments

0

If you change it a little

void updateDatabase() { getConnection createStatement executeStaement getResult } 

it will be definitely thread safe

1 Comment

Well, the question is "Is this sequence of calls thread safe". And not how to make this thread-safe

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.