0

I am looking for a development of a transaction framework, which needs to update the database tables concurrently.

In simple words, a single transaction should update concurrently around 8 independent tables, and the whole transaction should fail if any update thrown error.

Is there any way I can handle it concurrently,

Ie, 10 Threads update 10 Tables and if any update fails all the update should rollback.

Is there any framework which allows to me handle this scenario.

If you use JTA or Spring transaction which will be shared by same connection and defeat the purpose of concurrent update.

Or any way I can write using custom thread based solution.

assylias
  • 321,522
  • 82
  • 660
  • 783
user2413742
  • 63
  • 1
  • 1
  • 4
  • http://stackoverflow.com/questions/6952294/how-to-share-one-transaction-between-multi-threads?rq=1 "Multiple threads may concurrently be associated with the same global transaction." - JTA spec v1.1, section 3.2, page 13. – user2413742 Jun 03 '13 at 13:02

3 Answers3

0

Why would using JTA or Spring Transaction mean you'll use the same connection? If you configure a connection pool and connect to it correctly, surely you'll get a different connection for each thread that you use?

This just seems like an unusually configured distributed transaction to me, and my first attempt at this would be to use Spring and/or Hibernate. I think you'd just have to ensure that you were treating the transactions as distributed transactions.

DaveH
  • 7,187
  • 5
  • 32
  • 53
0

The framework is JTA.

It depends from your database whether you can use one connection for all threads. Details can be found here. So in the general case you need a connection for each thread.

If you use an XA data source, you could try to run the concurrent threads under control of a JTA transaction.

This is a lot of complexity, and it takes time to prepare the threads, so it's probably only useful, if the updates take a long time, the affected tables are independent, and you have enough CPUs in your database server.

Update

Regarding transaction propagation, here you can find some thoughts on it.

Community
  • 1
  • 1
Beryllium
  • 12,808
  • 10
  • 56
  • 86
0

You can use the standard JDBC. JDBC allows you to share a single Connection among multiple threads. To make several threads work in one transaction you should

  • create a java.sql.Connection / or take it from pool
  • turn autocommit off
  • run concurrent tasks with the same connection
  • wait for the tasks to finish
  • commit if all tasks finished successfully; rollback otherwise
  • close connection

It is also possible to use Spring JDBC if you use Spring's SingleConnectionDataSource

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Thanks Evgeniy Dorofeev. Your answer seems like a normal solution for a mult table transactions and it does not adress the concurrency part. If you use the same connection, the whole purpose of concurrency is gone. JDBC will serialise and it will update sequential manner only. – user2413742 Jun 03 '13 at 12:36
  • If you think that if task 2 stmt.executeUpdate will be blocked until task 1 stmt.executeUpdate returns you are wrong. Test it. – Evgeniy Dorofeev Jun 03 '13 at 13:58