mysql - No transaction is currently active error with eclipselink-2.5.2 Spring 4.1.1 -
solved : go through comments/chat
i using spring 4.1.1 annotation based configuration. while trying utilize eclipselink spring, error on merge
method stating no transaction active
following stacktrace:
javax.persistence.transactionrequiredexception: exception description: no transaction active @ org.eclipse.persistence.internal.jpa.transaction.entitytransactionwrapper.throwchecktransactionfailedexception(entitytransactionwrapper.java:87) @ org.eclipse.persistence.internal.jpa.transaction.entitytransactionwrapper.checkfortransaction(entitytransactionwrapper.java:50) exception occured @ : javax.persistence.transactionrequiredexception: exception description: no transaction active @ org.eclipse.persistence.internal.jpa.entitymanagerimpl.checkfortransaction(entitymanagerimpl.java:2041) @ org.eclipse.persistence.internal.jpa.entitymanagerimpl.flush(entitymanagerimpl.java:863) @ com.cdl.box.dao.genericdao.merge(genericdao.java:61) @ com.cdl.box.dao.genericdao$$fastclassbyspringcglib$$9e11ea6a.invoke(<generated>) @ org.springframework.cglib.proxy.methodproxy.invoke(methodproxy.java:204) @ org.springframework.aop.framework.cglibaopproxy$cglibmethodinvocation.invokejoinpoint(cglibaopproxy.java:717) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:157) @ org.springframework.dao.support.persistenceexceptiontranslationinterceptor.invoke(persistenceexceptiontranslationinterceptor.java:136) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.aop.framework.cglibaopproxy$dynamicadvisedinterceptor.intercept(cglibaopproxy.java:653) @ com.cdl.box.dao.persondao$$enhancerbyspringcglib$$51ee0a8f.merge(<generated>) @ com.cdl.box.service.person.personservice.addperson(personservice.java:23) @ com.cdl.box.service.person.personservice$$fastclassbyspringcglib$$dbc06f33.invoke(<generated>) @ org.springframework.cglib.proxy.methodproxy.invoke(methodproxy.java:204) @ org.springframework.aop.framework.cglibaopproxy$cglibmethodinvocation.invokejoinpoint(cglibaopproxy.java:717) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:157) @ org.springframework.transaction.interceptor.transactioninterceptor$1.proceedwithinvocation(transactioninterceptor.java:98) @ org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:266) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:95) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.aop.framework.cglibaopproxy$dynamicadvisedinterceptor.intercept(cglibaopproxy.java:653) @ com.cdl.box.service.person.personservice$$enhancerbyspringcglib$$35eaf713.addperson(<generated>) @ com.cdl.box.controller.personcontroller.add(personcontroller.java:55) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.springframework.web.method.support.invocablehandlermethod.invoke(invocablehandlermethod.java:215) @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:132) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:104) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlemethod(requestmappinghandleradapter.java:781) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:721) @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:83) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:943) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:877) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:966) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:868) @ javax.servlet.http.httpservlet.service(httpservlet.java:646) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:842) @ javax.servlet.http.httpservlet.service(httpservlet.java:727) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:503) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:170) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:950) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:421) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1070) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:316) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745)
webconfig.java
@enablewebmvc @configuration @componentscan("com.myapp") @propertysource(value = "classpath:application.properties") @enabletransactionmanagement(proxytargetclass=true) public class webconfig extends webmvcconfigureradapter{ @bean public static propertysourcesplaceholderconfigurer getpropertysourcesplaceholderconfigurer() { homecoming new propertysourcesplaceholderconfigurer(); } @bean public urlbasedviewresolver setupviewresolver() { urlbasedviewresolver resolver = new urlbasedviewresolver(); system.out.println("urlbasedviewresolver........"); resolver.setprefix("/web-inf/views/"); resolver.setsuffix(".jsp"); resolver.setviewclass(jstlview.class); homecoming resolver; } @bean multipartresolver multipartresolver() { multipartresolver resolver = new standardservletmultipartresolver(); homecoming resolver; } @override public void addresourcehandlers(resourcehandlerregistry registry) { system.out.println("in resouce handler"); registry.addresourcehandler("/resources/**").addresourcelocations("/resources/defaulttheme/"); } }
dbconfig.java
import java.util.properties; import javax.sql.datasource; import org.eclipse.persistence.platform.database.databaseplatform; import org.eclipse.persistence.platform.database.mysqlplatform; import org.springframework.beans.factory.annotation.value; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.enableloadtimeweaving; import org.springframework.dao.annotation.persistenceexceptiontranslationpostprocessor; import org.springframework.instrument.classloading.instrumentationloadtimeweaver; import org.springframework.instrument.classloading.simpleloadtimeweaver; import org.springframework.orm.jpa.jpadialect; import org.springframework.orm.jpa.jpatransactionmanager; import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; import org.springframework.orm.jpa.vendor.eclipselinkjpadialect; import org.springframework.orm.jpa.vendor.eclipselinkjpavendoradapter; import org.springframework.transaction.platformtransactionmanager; import org.springframework.transaction.annotation.enabletransactionmanagement; import com.jolbox.bonecp.bonecpdatasource; @configuration @enabletransactionmanagement(proxytargetclass = true) @enableloadtimeweaving public class dbconfig { @value("${jdbc.driverclassname}") private string driverclassname; @value("${jdbc.url}") private string url; @value("${jdbc.username}") private string username; @value("${jdbc.password}") private string password; @value("${eclipselink.persistenceunitname}") private string persistenceunitname; @bean() public datasource getdatasource() { bonecpdatasource ds = new bonecpdatasource(); system.out.println("driver name : " + driverclassname); system.out.println("url : " + url); ds.setdriverclass(driverclassname); ds.setjdbcurl(url); ds.setusername(username); ds.setpassword(password); ds.setmaxconnectionsperpartition(5); ds.setminconnectionsperpartition(2); ds.setacquireincrement(2); homecoming ds; } @bean public localcontainerentitymanagerfactorybean entitymanagerfactorybean() { localcontainerentitymanagerfactorybean em = new localcontainerentitymanagerfactorybean(); em.setdatasource(getdatasource()); em.setjpadialect(jpadialect()); em.setpackagestoscan("com.myapp.model", "com.myapp.model.person"); em.setpersistenceunitname(persistenceunitname); databaseplatform dp = new mysqlplatform(); em.setjpavendoradapter(geteclipselinkjpavendoradapter()); //em.setloadtimeweaver(new simpleloadtimeweaver()); em.setloadtimeweaver(new instrumentationloadtimeweaver()); homecoming em; } @bean public eclipselinkjpavendoradapter geteclipselinkjpavendoradapter() { eclipselinkjpavendoradapter vendoradapter = new eclipselinkjpavendoradapter(); vendoradapter.setdatabaseplatform("org.eclipse.persistence.platform.database.mysqlplatform"); vendoradapter.setgenerateddl(false); vendoradapter.setshowsql(true); homecoming vendoradapter; } @bean public platformtransactionmanager transactionmanager() { jpatransactionmanager transactionmanager = new jpatransactionmanager(); transactionmanager.setentitymanagerfactory(entitymanagerfactorybean().getobject()); homecoming transactionmanager; } @bean public persistenceexceptiontranslationpostprocessor persistenceexceptiontranslationpostprocessor() { homecoming new persistenceexceptiontranslationpostprocessor(); } @bean public jpadialect jpadialect() { homecoming new eclipselinkjpadialect(); } }
webinitializer.java
import javax.servlet.multipartconfigelement; import javax.servlet.servletcontext; import javax.servlet.servletexception; import javax.servlet.servletregistration; import org.springframework.web.webapplicationinitializer; import org.springframework.web.context.contextloaderlistener; import org.springframework.web.context.support.annotationconfigwebapplicationcontext; import org.springframework.web.servlet.dispatcherservlet; public class webinitializer implements webapplicationinitializer{ public void onstartup(servletcontext servletcontext) throws servletexception { // todo auto-generated method stub system.out.println("in webapplicationinitializer....."); annotationconfigwebapplicationcontext rootcontext = new annotationconfigwebapplicationcontext(); // manage lifecycle of root application context servletcontext.addlistener(new contextloaderlistener(rootcontext)); servletcontext.setinitparameter("contextconfiglocation", "<none>"); rootcontext.register(dbconfig.class); rootcontext.register(webconfig.class); rootcontext.setservletcontext(servletcontext); servletregistration.dynamic servlet = servletcontext.addservlet("dispatcher", new dispatcherservlet(rootcontext)); servlet.setloadonstartup(1); servlet.addmapping("/"); servlet.setmultipartconfig(new multipartconfigelement("/img", 1024*1024*5, 1024*1024*5*5, 1024*1024)); } }
fetching info works perfectly. error occurs during insert/update using eclipselink.
i went through lot of posts & applied suggestions. unfortunately not working. not sure missing. see wrong configuration ? please suggest me doing wrong here.
edit:
following service class @transactional.
import java.util.list; import org.eclipse.persistence.queries.readallquery; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.propagation; import org.springframework.transaction.annotation.transactional; import com.myapp.dao.persondao; import com.myapp.model.person.person; @service @transactional(propagation=propagation.required) public class personservice { @autowired private persondao persondao; public void addperson(person person) { person p = persondao.merge(person); system.out.println(p.getcity()); } public void updateperson(person person) { persondao.persist(person); } public person getperson(int id) { homecoming persondao.findbyid(id); } public void deleteperson(int id) { persondao.deletebyid(id); } public list<person> getpersons(int first, int rows){ readallquery readallquery = new readallquery(person.class); readallquery.setjpqlstring("select object(per) person per"); homecoming persondao.executereadallquery(readallquery, first, rows); } }
with @transactional on service class next error while start tomcat
org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'personcontroller' defined in file [d:\hemraj-dev\apache-tomcat-7.0.56\webapps\yourbox\web-inf\classes\com\myapp\controller\personcontroller.class]: unsatisfied dependency expressed through constructor argument index 0 of type [com.myapp.service.person.personservice]: : error creating bean name 'personservice': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.myapp.dao.persondao com.myapp.service.person.personservice.persondao; nested exception java.lang.illegalargumentexception: can not set com.myapp.dao.persondao field com.myapp.service.person.personservice.persondao com.sun.proxy.$proxy25; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'personservice': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.myapp.dao.persondao com.myapp.service.person.personservice.persondao; nested exception java.lang.illegalargumentexception: can not set com.myapp.dao.persondao field com.myapp.service.person.personservice.persondao com.sun.proxy.$proxy25 @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:751) @ org.springframework.beans.factory.support.constructorresolver.autowireconstructor(constructorresolver.java:185) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.autowireconstructor(abstractautowirecapablebeanfactory.java:1133) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1036) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:505) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:476) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:229) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:298) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:193) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:725) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:757) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:480) @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:403) @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:306) @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:106) @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4992) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5492) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:150) @ org.apache.catalina.core.containerbase$startchild.call(containerbase.java:1575) @ org.apache.catalina.core.containerbase$startchild.call(containerbase.java:1565) @ java.util.concurrent.futuretask.run(futuretask.java:262) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ java.lang.thread.run(thread.java:745) caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'personservice': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.myapp.dao.persondao com.myapp.service.person.personservice.persondao; nested exception java.lang.illegalargumentexception: can not set com.myapp.dao.persondao field com.myapp.service.person.personservice.persondao com.sun.proxy.$proxy25 @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:334) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1204) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:538) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:476) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:229) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:298) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:193) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.findautowirecandidates(defaultlistablebeanfactory.java:1081) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1006) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:904) @ org.springframework.beans.factory.support.constructorresolver.resolveautowiredargument(constructorresolver.java:815) @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:743) ... 24 more caused by: org.springframework.beans.factory.beancreationexception: not autowire field: private com.myapp.dao.persondao com.myapp.service.person.personservice.persondao; nested exception java.lang.illegalargumentexception: can not set com.myapp.dao.persondao field com.myapp.service.person.personservice.persondao com.sun.proxy.$proxy25 @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:555) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:87) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:331) ... 36 more caused by: java.lang.illegalargumentexception: can not set com.myapp.dao.persondao field com.myapp.service.person.personservice.persondao com.sun.proxy.$proxy25 @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:164) @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:168) @ sun.reflect.unsafeobjectfieldaccessorimpl.set(unsafeobjectfieldaccessorimpl.java:81) @ java.lang.reflect.field.set(field.java:741) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:551) ... 38 more
edit2:
i have replaced new stacktarce on top of question no transaction active
error. next daos.
persondao.java:
import javax.persistence.query; import org.springframework.stereotype.repository; import org.springframework.transaction.annotation.propagation; import org.springframework.transaction.annotation.transactional; import com.myapp.model.person.person; @repository("persondao") public class persondao extends genericdao<person, integer> { @override public int deletebyid(integer id) { query q = getentitymanager().createquery( "delete employees employeeid = :id"); q.setparameter("id", id); homecoming q.executeupdate(); } }
genericdao.java
public class genericdao<e, id extends serializable>{ private final class<e> persistentclass; protected entitymanager entitymanager; private entitymanagerfactory emf; @persistencecontext(unitname = "cdlbox") private entitymanager em; @value("${eclipselink.persistenceunitname}") private string persistenceunitname; @suppresswarnings("unchecked") public genericdao() { this.persistentclass = (class<e>) ((parameterizedtype) getclass() .getgenericsuperclass()).getactualtypearguments()[0]; } protected class<e> getentityclass() { homecoming persistentclass; } protected entitymanager getentitymanager(){ homecoming em; } public e merge(e entity) { e e = null; try{ e = em.merge(entity); em.flush(); }catch (exception ex) { system.out.println("exception occured @ : " + ex); ex.printstacktrace(); } homecoming e; } public void persist(e entity) { em.persist(entity); } public void remove(e entity) { em.remove(entity); } public e findbyid(id id) { homecoming em.find(getentityclass(), id); } @suppresswarnings("unchecked") public list<e> executenamedquery(string queryname, map<string, object> parametermap) { query query = em.createnamedquery( queryname); if (parametermap != null) { (string param : parametermap.keyset()) { query.setparameter(param, parametermap.get(param)); } } homecoming query.getresultlist(); } public list<e> executenamedquery(string queryname) { homecoming executenamedquery(queryname, null); } @suppresswarnings("unchecked") public int getcount(string countfield, look expr) { reportquery query = new reportquery(getentityclass(), null); query.setselectioncriteria(expr); query.addcount("count", query.getexpressionbuilder().get(countfield) .distinct()); list<reportqueryresult> result = (list<reportqueryresult>) getsession().executequery(query); reportqueryresult reportqueryresult = result.get(0); bigdecimal count = (bigdecimal) reportqueryresult.getresults().get(0); homecoming count.intvalueexact(); } protected session getsession() { //entitymanagerimpl has imported org.eclipse.persistence.internal.jpa.entitymanagerimpl session session = ((entitymanagerimpl) em).getactivesession(); homecoming session; } public list<e> executereadallquery(readallquery readallquery, int first, int rows) { readallquery.setfirstresult(first); readallquery.setmaxrows(first + rows); homecoming executedatabasequery(readallquery); } @suppresswarnings("unchecked") public list<e> executedatabasequery(databasequery q) { list<e> resultlist = (list<e>) getsession().executequery(q); homecoming resultlist; } public int deletebyid(id id) { // todo auto-generated method stub homecoming 0; } }
edit3:
got this exception while using @persistencecontext
if using eclipselink , jotm
running on tomcat
in persistence.xml, guess forgot property
:
<properties> <property name="eclipselink.target-server" value="org.eclipse.persistence.transaction.jotm.jotmtransactioncontroller"/> </properties>
mysql spring spring-mvc jpa eclipselink
No comments:
Post a Comment