1

I have a LinkedBlockingQueue, and I would like to save all the elements to the database. I know I can save it one by one:

ObjectModel om = new ObjectModel();
om.save();

However, that means I have to loop through all the elements and do a new connection for each. Can I insert the entire list at once?

Random Human
  • 946
  • 1
  • 14
  • 31
gruuuvy
  • 2,028
  • 4
  • 31
  • 52
  • You can loop through all elements and call save(). That won't create a new connection each time though. Transactions in play are bound to the HTTP connection. – Chris Dail May 30 '12 at 20:09

3 Answers3

2

In play 1.x, when the server receives a request, play/jpa will open a transaction to you database. This transaction will automatically be rolled back if anything goes wrong, else it automatically commits when the response is sent to the client.

Therefor, even if you loop through your JPA entities and call save() on them one after another, they will all be executed with the same connection in the same transaction.

I would think it works the same way in Play 2, but honestly, I don't know.

aaberg
  • 2,253
  • 15
  • 14
1

The simple answer will not NO. Some one down the line will have to execute insert query for all individual objects.

You do not have to create new connection for each insert. You should create a connection and fire insert on the same connection in each iteration.

Vijay Shanker Dubey
  • 4,308
  • 6
  • 32
  • 49
  • There's no JPA equivalent of inserting multiple rows at once in MySQL? Because being able to insert multiple rows would speed up the query a lot more as oppose to inserting one row at a time. – gruuuvy May 30 '12 at 15:56
0

Maybe this question gets you further. If you know how to do what you want on the underlying DB, you also could use a native query (which you can create via the EntityManager), I suppose.

Community
  • 1
  • 1
msung
  • 3,562
  • 3
  • 19
  • 30