tags:

views:

58

answers:

2

HI,

Iam trying to insert batch of records at a time when any of the record fails to insert i need to trap that record and log that to my failed record maintanance table and then the insert should continue. Kindly help on how to do this.

A: 

If using a Spring or EJB container there is a simple trick which works very well : provide a LogService witn a logWarning(String message) method. The method must be annotated/configured with the REQUIRES_NEW transaction setting.

If not then you'll have to simulate it using API calls. Open a different connection for the logging, when you enter the method begin the transaction, before leaving commit the transaction.

When not using transactions for the insert, there is actually nothing special you need to do, as by default most database run in autocommit and commit after every statement.

Peter Tillemans
+1  A: 

This might interest you.

leonbloy
+1 for SAVEPOINTs. In addition, you may want to look at this: http://stackoverflow.com/questions/2370328/continuing-a-transaction-after-primary-key-violation-error/2372997#2372997 as my pseudocode is very close to the use case you describe.
Matthew Wood