1

I have 4 tables Each table is updated by 4 different procedures. Each procedure is called by java using 4 different threads. In each thread I am using same connection object(JDBC Datasource connection). After all threads have updated all the tables, then I am committing the connection in java. Data has been saved successfully.

Is this the right way to handle the transaction management using multithreading by sharing the same connection object?

Driver used: org.postgresql.Driver postgres jar version: 42.2.2.jar postgres server version: 10.6

Please provide your suggestions.

Regards, Rakesh Kumar

  • In general you should avoid concurrent access to JDBC connection from multiple threads. You may find this useful - https://stackoverflow.com/questions/46217941/jdbc-transactions-in-multi-threaded-environment – Kishore Kirdat Jul 11 '19 at 07:09
  • 1
    Not possible. A single connection can only run a single statement at a time. You will either need 4 connections or serialize the access to the connection. –  Jul 11 '19 at 07:31
  • Why not to use connection pool so that each thread will have it's own database connection, especially if threads insert data which is independent? – Ivan Jul 14 '19 at 12:46

0 Answers0