Best practice for when to start a new session/transaction for batch jobs using spring/hibernate and when to commit/flush the session?

First of all, Spring/bernate are not really intended for batch processing. Instead, check out either Talend or Pentaho (if you are into open source), or any of a huge (massive! ) variety of commercial tools.

Either of these tools can be used to automatically generate a lump of Java code that will do exactly what you need (including insert optimization, elegant error handling, etc).

First of all, Spring/bernate are not really intended for batch processing. Instead, check out either Talend or Pentaho (if you are into open source), or any of a huge (massive! ) variety of commercial tools.

Either of these tools can be used to automatically generate a lump of Java code that will do exactly what you need (including insert optimization, elegant error handling, etc). Ok, let's assume that you really, really want to make Spring/bernate do batch processing. You have a couple of different issues - first, the bernate session lifecycle means that objects loaded expect to be associated to the live session.

You can use the session flush() to force the changes to propagate to the database. Session close() will wipe everything out. Objects that are already loaded can only be reattached to a new session with difficulty (usually it's easier to just reload the object).

If you don't close()/flush() your session, eventually you will (probably) run out of memory. You can fix that by adding a bernate 2nd level cache... but that will just make things more complex and slow it down. There is no real reason not to just do each insertion within an independent do work, close).

It won't be as fast as a dedicated tool, but it's simple, will work fine, and is more or less as good as you'll get.

With regards to the batching requirment, please use Spring batching link this provides all the necessary batching facilities needed. Regarding the object loading issue, So a way around this which may not be the most optimal is - the batch class just calls a service to load all the IDs of those objects (long values) - and we pass this ID to a service method which will load that object from the DB by the ID and then do the processing on it. Seems correct.

First of all, Spring/Hibernate are not really intended for batch processing. Instead, check out either Talend or Pentaho (if you are into open source), or any of a huge (massive!) variety of commercial tools. Either of these tools can be used to automatically generate a lump of Java code that will do exactly what you need (including insert optimization, elegant error handling, etc).

With regards to the batching requirment, please use Spring batching link this provides all the necessary batching facilities needed.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions