2

Hi im trying to keep track of my Apache POI reading an excel file an importing to the database. I am persisting the imports progress (% Progress,time etc) to a separate table in the database so i can query the data import progress.

My problem is that the entity manager only persists the "progress" state once, and that is at the end of the method that loads the data into the database.

Java

    @Inject
        private EntityManager em;

    public void importTheData(String path){

        try 
        {

            FileInputStream file = new FileInputStream(path);
            wb = WorkbookFactory.create(file);
        } catch (Exception ex) {}   

        Sheet sheet = wb.getSheetAt(0);

        for (int i = 1; i <= sheet.getLastRowNum(); i++) {

            try {
                row = sheet.getRow(i);

                Table theTable = new Table();
                theTable.setDate(row.getCell(0).getDateCellValue());
    theTable.setDate1(row.getCell(1).getDateCellValue());
    theTable.setDate2(row.getCell(2).getDateCellValue());

    bdt.add(theTable);

}

public void importtheTable() {

    for(int i=0; i<bdt.size();i++)
    {

        em.persist(bdt.get(i));

        if(i%2000 == 0){
            em.flush();
            em.clear();
        }
    }

    }

The method setProgress(path,(100* i/theTable.getLastRowNum())) attempts to persists the progress state to a seperate table, but this only happens once (At the end)

Can anybody help me out?

Thanks

2
  • 2
    How many records are in the excel file? Apr 12, 2013 at 20:32
  • 30000 records are in the excel sheet Apr 12, 2013 at 20:42

2 Answers 2

3

It depends on the scope of the transaction, do you use the annotations to define this? If yes, this explains why everything is done at the end.

You will need to begin / end the transaction within this method if you want it otherwise.

2
  • The bean class that has the importTheData() method injects my EntityManager which in turn persists into the database - i dont have any @Transaction anywhere.. Apr 12, 2013 at 20:53
  • Ok - found the solution - I put @TransactionManagement(TransactionManagementType.BEAN) at the top of this class and injected @Resource UserTransaction utx whist calling utx.begin(); before the setProgress() method and utx.commit(); after - Cheers P.Lalonde Apr 12, 2013 at 21:00
1

It depends on the entityManger you have created.

If u have write code in loop like entityManager.gettransaction.commit() then u can get get updates in the Database.

but if u have done it after loop then it will commit all at once.

Also the Type of transaction you have specified on method matters.

4
  • I think i cant apply that method "em.getTransaction().commit();" as my resource type is JTA - im going to put up a new question to clarify further i think with my persistence.xml settings etc. When i try that way i get this exception "A JTA EntityManager cannot use getTransaction()" Apr 15, 2013 at 14:29
  • can you please show me how you have created entitiManager instance
    – JavaSun
    Apr 15, 2013 at 14:34
  • Just updated there - i have also tried @Resource UserTransaction utx ... then utx.begin(); and utx.commit(); but get get errors also Apr 15, 2013 at 14:38
  • also storing the excel values read in into an array and persist that array through a loop which might help which batching? Apr 15, 2013 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.