java - Configure transaction in Spring 4.1.5 without XML -
i'm writing application connects oracle database. call function db inserts new records table. , after callback can decide want do: commit or rollback.
unfortunalety i'm new in spring, have problems configuration. , what's more want make configuration in java class, not in xml. , here need help.
updated code:
applicationconfig code:
@configuration @enabletransactionmanagement @componentscan("hr") @propertysource({"classpath:jdbc.properties", "classpath:functions.properties", "classpath:procedures.properties"}) public class applicationconfig { @autowired private environment env; @bean(name="datasource") public datasource datasource() { basicdatasource datasource = new basicdatasource(); datasource.setdriverclassname(env.getproperty("jdbc.driver")); datasource.seturl(env.getproperty("jdbc.url")); datasource.setusername(env.getproperty("jdbc.username")); datasource.setpassword(env.getproperty("jdbc.password")); datasource.setdefaultautocommit(false); return datasource; } @bean public jdbctemplate jdbctemplate(datasource datasource) { jdbctemplate jdbctemplate = new jdbctemplate(datasource); jdbctemplate.setresultsmapcaseinsensitive(true); return jdbctemplate; } @bean(name="txname") public platformtransactionmanager txmanager() { datasourcetransactionmanager txmanager = new datasourcetransactionmanager(); txmanager.setdatasource(datasource()); return txmanager; } }
i have dao , service, both implements proper interface.
service implementation:
@service public class humanresourcesserviceimpl implements humanresourcesservice { @autowired private humanresourcesdao hrdao; @override public string generatedata(int rowsnumber) { return hrdao.generatedata(rowsnumber); } @override @transactional("txname") public void shouldcommit(boolean docommit, connection connection) throws sqlexception { hrdao.shouldcommit(docommit, connection); } }
dao implementation:
@repository public class humanresourcesdaoimpl implements humanresourcesdao { private jdbctemplate jdbctemplate; private simplejdbccall generatedata; @autowired public humanresourcesdaoimpl(jdbctemplate jdbctemplate, environment env) { this.jdbctemplate = jdbctemplate; generatedata = new simplejdbccall(jdbctemplate) .withprocedurename(env.getproperty("procedure.generatedata")); } @override public string generatedata(int rowsnumber) { hashmap<string, object> params = new hashmap<>(); params.put("i_rowsnumber", rowsnumber); map<string, object> m = generatedata.execute(params); return (string) m.get("o_execution_time"); } @override @transactional("txname") public void shouldcommit(boolean docommit, connection connection) throws sqlexception { if(docommit) { connection.commit(); } else { connection.rollback(); } } }
main class code:
public class main extends application implements initializable { @override public void initialize(url url, resourcebundle resourcebundle) { applicationcontext context = new annotationconfigapplicationcontext(applicationconfig.class); hrservice = context.getbean(humanresourcesservice.class); basicdatasource ds = (basicdatasource)context.getbean("datasource"); connection connection = ds.getconnection(); //do , call //hrservice.generatedata //do , call //hrservice.shouldcommit(true, connection); //which commit or rollback generated data previoues callback } }
update:
i think problem connection, because statement:
this.jdbctemplate.getdatasource().getconnection();
creates new connection, there nothing commit or rollback. still can't figure why doesn't work properly. no errors, no new records... wierd, when debuged connection.commit(); found out in delegatingconnection.java, parameter this has proper connection, there like:
_conn.commit();
and _conn has different connection. why?
should in way synchronize connection 2 methods or what? or 1 connection? honest, i'm not sure how works exactly. 1 connection , callbacks stored procedures in connection or maybe each callback new connection created?
real question how commit or rollback data previous callback insert table?
one easy way annotate method @transactional
:
@transactional public void mybeanmethod() { ... if (!docommit) throw new illegalstateexception(); // unchecked }
and spring roll database changes back.
remember add @enabletransactionmanagement
spring application/main class
Comments
Post a Comment