java - Spring jpa no transaction in progress exception -


i'm new spring , don't understand why i'm facing issue.
want save account object postgres database, connection ok flush() method invocation causes exception. this issue looks situation don't use lots of xml configure app, i'm confused , don't do.
here's code :

initializer.java

public class initializer implements webapplicationinitializer {      private static final string dispatcher_servlet_name = "dispatcher";      @override     public void onstartup(servletcontext servletcontext) throws servletexception {         annotationconfigwebapplicationcontext ctx = new annotationconfigwebapplicationcontext();         ctx.register(webappconfig.class);         ctx.register(securityconfig.class);         ctx.register(jpaconfig.class);         servletcontext.addlistener(new contextloaderlistener(ctx));         ctx.setservletcontext(servletcontext);          dynamic servlet = servletcontext.addservlet(dispatcher_servlet_name, new dispatcherservlet(ctx));         servlet.addmapping("/");         servlet.setloadonstartup(1);     } } 

jpaconfig.java

@configuration @enabletransactionmanagement public class jpaconfig {     @bean     public localcontainerentitymanagerfactorybean entitymanagerfactory(){         localcontainerentitymanagerfactorybean em = new localcontainerentitymanagerfactorybean();         em.setdatasource(datasource());         em.setpackagestoscan(new string[] { "me.owl.persistence.model"});         jpavendoradapter vendoradapter = new hibernatejpavendoradapter();         em.setjpavendoradapter(vendoradapter);         em.setjpaproperties(additionalproperties());         return em;     }     @bean     properties additionalproperties() {         properties properties = new properties();         properties.setproperty("hibernate.dialect", "org.hibernate.dialect.postgresqldialect");         properties.setproperty("hibernate.hbm2ddl.auto", "create");         properties.setproperty("hibernate.format_sql", "true");         return properties;     }     @bean     public platformtransactionmanager transactionmanager(entitymanagerfactory emf){         jpatransactionmanager transactionmanager = new jpatransactionmanager();         transactionmanager.setentitymanagerfactory(emf);         return transactionmanager;     }      @bean     public persistenceexceptiontranslationpostprocessor exceptiontranslation(){            return new persistenceexceptiontranslationpostprocessor();     }     @bean     public datasource datasource() {         drivermanagerdatasource datasource = new drivermanagerdatasource();         datasource.setdriverclassname("org.postgresql.driver");         datasource.seturl("jdbc:postgresql://babar.elephantsql.com:5432/test");         datasource.setusername("root");         datasource.setpassword("password");         return datasource;     } } 

securityconfig.java

@configuration @enablewebsecurity @enableglobalmethodsecurity(securedenabled = true) public class securityconfig extends websecurityconfigureradapter {     @autowired     private userdetailsserviceimpl userdetailsservice;      @autowired     public void registerglobalauthentication(authenticationmanagerbuilder auth) throws exception {         auth             .userdetailsservice(userdetailsservice)             .passwordencoder(getpasswordencoder());     }     @override     protected void configure(httpsecurity http) throws exception { ... }     @bean     public passwordencoder getpasswordencoder(){ ... } } 

webappconfig.java

@configuration @enablewebmvc @componentscan({"me.owl"}) public class webappconfig extends webmvcconfigureradapter {      @override     public void addresourcehandlers(resourcehandlerregistry registry) {        registry.addresourcehandler("/pages/**").addresourcelocations("/pages/");     }     @bean     public userdetailsservice getuserdetailsservice() {         return new userdetailsserviceimpl();     }     @bean     public userservice userservice() {         return new hibernateuserserviceimpl();     }     @bean     public urlbasedviewresolver setupviewresolver() {         urlbasedviewresolver resolver = new urlbasedviewresolver();         resolver.setprefix("/pages/");         resolver.setsuffix(".jsp");         resolver.setviewclass(jstlview.class);         return resolver;     }     @override     public void addinterceptors(interceptorregistry registry) {         localechangeinterceptor localechangeinterceptor = new localechangeinterceptor();         localechangeinterceptor.setparamname("lang");         registry.addinterceptor(localechangeinterceptor);     }     @bean     public localeresolver localeresolver() {         cookielocaleresolver cookielocaleresolver = new cookielocaleresolver();         cookielocaleresolver.setdefaultlocale(locale.english);         return cookielocaleresolver;     }     @bean     public messagesource messagesource() {         reloadableresourcebundlemessagesource messagesource =             new reloadableresourcebundlemessagesource();         messagesource.setbasename("classpath:messages");         messagesource.setusecodeasdefaultmessage(true);         messagesource.setdefaultencoding("utf-8");         messagesource.setcacheseconds(0);         return messagesource;     } } 

web.xml

<filter>     <filter-name>springsecurityfilterchain</filter-name>     <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class> </filter>  <filter-mapping>     <filter-name>springsecurityfilterchain</filter-name>     <url-pattern>/*</url-pattern> </filter-mapping> 

registrationcontroller.java

@controller public class registrationcontroller {     @autowired     private userservice userservice;     @autowired     private passwordencoder passwordencoder;      @requestmapping(value = "/register", method = requestmethod.get)     public string registrationpage(modelmap model) {         userdto user = new userdto();         model.addattribute("user", user);         return "register";     }     @requestmapping(value = "/register", method = requestmethod.post)     public string register(@valid @modelattribute("user") userdto user, bindingresult result) {         if(result.haserrors()) {             return "register";         }         account newuser = new account();         newuser.setemail(user.getemail());         newuser.setpassword(passwordencoder.encode(user.getpassword()));         newuser.setregistration(new date());         newuser.setlastlogin(newuser.getregistration());         userservice.save(newuser);         return "success";     } } 

seems spring doesn't see @transactional annotation on save() method in hibernateuserserviceimpl class.
hibernateuserserviceimpl.java

public class hibernateuserserviceimpl implements userservice {     @autowired     private userrepository repository;      public hibernateuserserviceimpl() {}     @override     @transactional     public account save(account user) {         repository.save(user);         return user;     }    ... } 

and code causes exception
userrepositoryimpl.java

@repository public class userrepositoryimpl implements userrepository {     @persistencecontext     private entitymanager em;      public userrepositoryimpl() {}     @override     public account save(account user) {         em.persist(user);         em.flush();         return user;     }     ... } 

here's stack trace

org.springframework.web.util.nestedservletexception: request processing failed; nested exception     org.springframework.dao.invaliddataaccessapiusageexception: no transaction in progress; nested exception javax.persistence.transactionrequiredexception: no transaction in progress org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:943) org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:833) javax.servlet.http.httpservlet.service(httpservlet.java:646) org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:807) javax.servlet.http.httpservlet.service(httpservlet.java:727) ... 

well, i'm not sure best decision fixed issue adding @transactional annotation on save() method in userrepositoryimpl class.
thanks @arpit comment.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -