java ee - Transaction propagation from CDI to EJB -
i want submit form file, , other simple fields. because sending file , saving in filesystem typical web tier, i'm doing in cdi managed bean. cdi managed bean call ejb store other data in database. saving file fail, annotated cdi bean's method @transactional (jee7). i'm expecting rollback ejb's transaction when saving file fail, not happen. normal behavior? how rollback ejb's transaction in case?
@named @javax.faces.view.viewscoped public class lecturectrl { @inject lecturesfacade lecturesfacade; @getter @setter uploadedfile paper; @getter @setter lecture lecture; @transactional(rollbackon={filenotfoundexception.class}) public void create(lecture _lecture) throws messagingexception, ioexception { try{ long _lectureid = lecturesfacade.create( _lecture ); //args: file, path, constant filename "abstract" webutils.handlefileupload( this.paper, "conferences/"+lecture.getconference().getid()+"/lectures/"+_lectureid, "abstract" ); }catch(exception e){ system.out.println(e); throw e; } } public string onsubmitnewlecture(){ //jsf-specific code this.create(this.lecture); //jsf-specific code } } @stateless public class lecturesfacade { @persistencecontext entitymanager em; @inject emailsubsystem emailsubsystem; public long create( lecture lecture ) throws messagingexception { em.persist(lecture); em.flush(); em.refresh(lecture); long id = lecture.getid(); emailsubsystem.sendemailonnewlecture(lecture); return id; } }
exception:
info: java.io.filenotfoundexception: c:\konferencje\files\conferences\3\lectures\20\abstract (access denied) @ java.io.fileoutputstream.open(native method) @ java.io.fileoutputstream.<init>(fileoutputstream.java:212) @ java.io.fileoutputstream.<init>(fileoutputstream.java:165) @ backingbeans.webutils.handlefileupload(webutils.java:154) @ backingbeans.lecturectrl.create(lecturectrl.java:56) ...
when webutils.handlefileupload
fails, want rollback changes made lecturesfacade.create
.
edit
i have annotated lecturefacade.create
method @transactionattribute(transactionattributetype.mandatory)
, thrown me javax.ejb.ejbtransactionrequiredexception
so looks cdi transaction not propagated ejb transaction.
the problem need define on checked exceptions rollback transactions. @transactional annotation has field called rollbackon set this
so try annotation @transactional(rollbackon={filenotfoundexception.class})
- haven't tried code, there might typo. reason why need specify because filenotfoundexception checked exception.
please read documentation linked, 1 of common errors while using container managed transactions.
edit
that's strange. can try adding ioexception rollbackon
? if doesn't fix check for:
- does db support transactions? (eg. mysql myisam doesn't support transactions)
- is db connection configured use transactions? (on drivers can specify perform autocommit on each sql statement)
- is container configured properly? unfortunately, didn't add full stacktrace, cannot see if container did create transaction proxy
lecturectrl
- do have more 1 datasource in application? works if have configured jta (which mentioned in tags)
Comments
Post a Comment