java - Spring @transactional doesn't work -


i'm using java configuration, work fine except @transactional, took me serval days try figure out, have no idea why doesn't work, great appreciate.

in usercontroller.java, try call userservice.testtransactional(user, request);

testtransaction() testing method in userserviceimpl, "long.valueof("throw runtimeexception");" line going throw exception in method, user added anyway, it's supposed roll back, user record persists

usercontroller.java

@controller public class usercontroller {      @resource(name="userservice")     protected userservice userservice;      @requestmapping(value = "/register", method = requestmethod.post)     public string processregister(model model, @modelattribute registerform registerform, httpservletrequest request) throws exception {          userservice.testtransactional(user, request);          return "account/register_success";     } } 

userserviceimpl.java

@service("userservice") public class userserviceimpl implements userservice {      @resource     private userdao userdao;      @transactional     public void testtransaction(user user, httpservletrequest request) throws exception {         long userid = userdao.add(user);         long.valueof("throw runtimeexception");     } } 

below configurations

web.xml

<!-- java-based annotation-driven spring container definition --> <context-param>     <param-name>contextclass</param-name>     <param-value>org.springframework.web.context.support.annotationconfigwebapplicationcontext</param-value> </context-param>  <!-- location of java @configuration classes configure components makeup application --> <context-param>     <param-name>contextconfiglocation</param-name>     <param-value>com.myapp.config</param-value> </context-param>   <!-- secures application --> <filter>     <filter-name>shirofilter</filter-name>     <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class> </filter> <filter-mapping>     <filter-name>shirofilter</filter-name>     <url-pattern>/*</url-pattern>     <dispatcher>request</dispatcher>     <dispatcher>forward</dispatcher>     <dispatcher>include</dispatcher>     <dispatcher>error</dispatcher>  </filter-mapping>  <!-- handles requests application --> <servlet>     <servlet-name>appservlet</servlet-name>     <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>     <init-param>         <!-- no explicit configuration file reference here: configured in root container simplicity -->                <param-name>contextconfiglocation</param-name>         <param-value></param-value>     </init-param>     <load-on-startup>1</load-on-startup> </servlet>  <servlet-mapping>     <servlet-name>appservlet</servlet-name>     <url-pattern>/</url-pattern> </servlet-mapping>  <!-- creates spring container shared servlets , filters --> <listener>     <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> 

appconfig.java

@configuration  // specifies class configuration @enablewebmvc //enables use spring's annotations in code @componentscan(basepackages = {"com.myapp.account"}) // specifies package scan public class appconfig {     ... 

dataconfig.java

@configuration @enabletransactionmanagement public class dataconfig {      @bean(destroymethod="close")     public combopooleddatasource datasource() throws propertyvetoexception {         ...         return cpds;     }      @bean     public namedparameterjdbctemplate jdbctemplate() throws propertyvetoexception {         return new namedparameterjdbctemplate(datasource());     }      @bean     public datasourcetransactionmanager transactionmanager() throws propertyvetoexception {         datasourcetransactionmanager dstm = new datasourcetransactionmanager(datasource());         return dstm;     }       @bean     public userdao userdao() throws propertyvetoexception {         return new userdao(jdbctemplate());     } 

the problem haven't specified @enabletransactionmanagement on @configuration class component scanning , creating userserviceimpl bean. assuming class in com.myapp.account, configuration class component scans should have @enabletransactionmanagement. change dataconfig or appconfig appropriately.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -