java - Spring:Propagation.REQUIRED not working -


i inserting records in couple of tables namely dept , emp. if dept table created want insert records in emp table. also, if of insert in emp fails, want rollback transaction includes both rollback emp dept tables.

i tried using propagation.required shown below:

java file

public void saveemployee(employee empl){     try {         jdbctemplate.update("insert emp values(?,?,?,?,?)",empl.getempid(),empl.getempname(),                 empl.getdeptid(),empl.getage(),empl.getsex());     } catch (dataaccessexception e) {         e.printstacktrace();     }  }  @transactional(propagation=propagation.required) public void saverecords(){     savedepartment(dept);     saveemployee(empl);      } 

context.xml

<tx:annotation-driven transaction-manager="transactionmanager" proxy-target-class="true"/>  <bean id="transactionmanager"     class="org.springframework.jdbc.datasource.datasourcetransactionmanager">             <property name="datasource" ref="datasource" /> </bean> 

problem:

even if insertion in emp table fails, dept insertion getting persisted don't want. want rollback everything.

please suggest.

the problem catch block. since exception catched tx don't rollback.

you must throw exception :

public void saveemployee(employee empl){     try {         jdbctemplate.update("insert emp values(?,?,?,?,?)",empl.getempid(),empl.getempname(),                 empl.getdeptid(),empl.getage(),empl.getsex());     } catch (dataaccessexception e) {         e.printstacktrace();         throw e;     }  } 

and way, semantic of progation.required means : create new tx if don't exists or use existing 1 if there tx running.


following comment here suggestion see effect of new tx :

@transactional(propagation=propagation.requires_new) public void saveemployee(employee empl){     try {         jdbctemplate.update("insert emp values(?,?,?,?,?)",empl.getempid(),empl.getempname(),                 empl.getdeptid(),empl.getage(),empl.getsex());     } catch (dataaccessexception e) {         e.printstacktrace();         throw e;     }  }  @transactional(propagation=propagation.required) public void saverecords(){     savedepartment(dept);     try{        saveemployee(empl);     }catch(exception e){logger.log("fail save emp !");}      } 

the key point see effect of requires_new catch exception around saveemployee. if don't catch : exception propagate in other tx (the 1 started when entering saverecords() ) , rollback too.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -