package net.benas.cb4j.tutorials.library;

import net.benas.cb4j.core.api.RecordProcessingException;
import net.benas.cb4j.core.api.RecordProcessor;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

/**
 * A book processor that inserts Book objects in database.<br/>
 *
 * <strong>This implementation is kept simple for demonstration purpose. In production environment, one may define a
 * commit interval to avoid performance issues of committing the transaction after each record insertion.</strong>
 *
 * @author benas (md.benhassine@gmail.com)
 */
public class BookProcessor implements RecordProcessor<Book> {

    public void preProcessRecord(Book book) throws RecordProcessingException {
        DatabaseUtil.getCurrentSession().beginTransaction();
    }

    public void processRecord(Book book) throws RecordProcessingException {
        try {
            DatabaseUtil.getCurrentSession().saveOrUpdate(book);
        } catch (HibernateException e) {
            DatabaseUtil.getCurrentSession().getTransaction().rollback();
            throw new RecordProcessingException(e.getMessage());
        }
    }

    public void postProcessRecord(Book book) throws RecordProcessingException {
        Transaction transaction = DatabaseUtil.getCurrentSession().getTransaction();
        if (transaction.isActive() && !transaction.wasRolledBack()) {
            transaction.commit();
        }
    }

}

@@ -113,36 +113,44 @@
 benas.cb4j.core.
-api.
+impl.Default
 RecordProcessor;
@@ -148,16 +148,20 @@
 rocessor
+Impl
 ;%0Aimport
@@ -198,42 +198,8 @@
 ion;
-%0Aimport org.hibernate.Transaction;
 %0A%0A/*
@@ -580,19 +580,23 @@
 sor 
-implement
+extend
 s 
+Default
 Reco
@@ -606,16 +606,20 @@
 rocessor
+Impl
 %3CBook%3E %7B
@@ -922,16 +922,88 @@
 (book);%0A
+            DatabaseUtil.getCurrentSession().getTransaction().commit();%0A
         
@@ -1105,24 +1105,24 @@
 rollback();%0A
+
             
@@ -1195,292 +1195,6 @@
  %7D%0A%0A
-    public void postProcessRecord(Book book) throws RecordProcessingException %7B%0A        Transaction transaction = DatabaseUtil.getCurrentSession().getTransaction();%0A        if (transaction.isActive() && !transaction.wasRolledBack()) %7B%0A            transaction.commit();%0A        %7D%0A    %7D%0A%0A
 %7D%0A

update library tutorial : commit transaction in processRecord method