understanding transactions and flushMode=MANUAL
gonorrhea Mar 19, 2009 12:08 AMI have the following Seam SFSB component which is acting as a backing bean for a JSF. It loops thru the List of serialNumber variables entered in the JSF form and persists records to the db table.
Pertinent code:
@Begin(join=true, flushMode=FlushModeType.MANUAL) @TransactionAttribute(TransactionAttributeType.REQUIRED) public void searchSerialNumbers() { //parse serial numbers from HtmlInputTextarea control.... List<String> serialNumberList = parseSerialNumber(); if (serialNumberList != null && serialNumberList.size() > 0) { for (String serialNumber : serialNumberList) //persist records one serialNumber at a time... { this.persistA(); //cause ArrayIndexOutOfBoundsException to be thrown String[] sArray = new String[4]; String s = sArray[10]; this.persistB(); } } } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) private void persistA() { TestTransactions testTransactions = new TestTransactions(); testTransactions.setSerialNumber(serialNumber); testTransactions.setAddedDate(new Date()); entityManager.persist(testTransactions); entityManager.flush(); } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) private void persistB() { TestTransactions testTransactions = new TestTransactions(); testTransactions.setSerialNumber(serialNumber); testTransactions.setAddedDate(new Date()); entityManager.persist(testTransactions); entityManager.flush(); }
Please explain why there are no records persisted to the db table at the end of persistA() when flush() is called and presumably the transaction is committed. Maybe it's not committed but I was expecting it would be b/c of the REQUIRES_NEW annotation for the method.
I need to be able to insert the records prior to any exception being thrown and display error msg's for the ones that failed (after the exception) or more likely were not attempted due to the exception. In other words, the typical ACID properties (specifically atomicity) don't apply; it's not an all-or-nothing transaction. I don't want the user to be forced to re-enter a whole bunch of data values in the JSF form for the records prior to the exception. Is this possible to achieve with Seam/Hibernate/JPA? If yes, how? thx.