spring hibernate manuel de commettre
quelqu'un peut-il aider pour cela?
actuellement j'ai rencontré quelques problèmes, je suis en utilisant spring,jpa,hibernate.
là est le problème, quand j'essaie d'enregistrer plusieurs objets, puis enrouler manuellement dos comme quand solrj de retour faux.
j'ai essayé le suivant:
1. un usertransaction (exception: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException
)
2.entitymanager.gettransaction().begin
(exception: partagé manager)
merci, si vous pouvez m'aider à comprendre.
en-tête:
@Service("jpaWallPostService")
@Repository
public class WallPostServiceImpl implements WallPostService {
//........
@PersistenceContext
private EntityManager em;
======================================
@Transactional(readOnly=false)
public boolean save(String solrURL,int userid,wall_post wallPost,List<String> groupsid,List<String> hashtag){
try{
em.getTransaction().begin();//***error occur on here*** and stop here
//...............proccess
boolean ok = SolrJ.IndexingWallPost(solrURL,userid, wallPost.getTitle(), wallPost.getMessage(), hashtag,groupsid);
if(!ok)
em.getTransaction().rollback();
em.getTransaction().commit();
return true;
}
catch(Exception e)
{
em.getTransaction().rollback();
return false;
}
}
voici le log d'erreur
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/opscentral] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead] with root cause
java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:198)
at $Proxy42.getTransaction(Unknown Source)
at sg.com.innovax.opscentralv5.table.service.impl.WallPostServiceImpl.save(WallPostServiceImpl.java:230)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy49.save(Unknown Source)
at sg.com.innovax.opscentralv5.UserHomeController.newWall_post(UserHomeController.java:171)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
at org.springframework.security.config.debug.DebugFilter.doFilterInternal(DebugFilter.java:45)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
post le journal d'erreur complet
OriginalL'auteur Stupidfrog | 2013-04-03
Vous devez vous connecter pour publier un commentaire.
Bonne façon est de supprimer toutes les manuel de gestion de la transaction lors de l'utilisation de l' @Transactionnelle. Dans un bon CMT contexte, vous ne devriez pas manuellement interagir avec les transactions, les @transactional assurer que votre méthode sera exécutée à l'intérieur d'une transaction, propage le contexte de transaction à déléguer l'appel et s'engage à la fin de l'exécution ou de restauration dans le cas de l'exception levée. Les seules choses que vous pouvez faire est de forcer la restauration à l'aide de
setRollbackOnly()
sur leEJBContext
Voir : est-il un moyen de forcer les opérations de restauration sans rencontrer une exception?
Sinon, si vous préférez gérer manuellement la transaction :
Définir un transactionManager bean dans votre ressort du contexte à l'aide de votre entityManagerFactory.
et de l'utiliser de cette façon :
Comment gérez-vous l'annotation de la manipulation ? N'avez-vous pas quelque chose comme ceci : <tx:annotation-driven gestionnaire de transactions="transactionManager" /> <context:annotation-config />
oui, je fais aussi sont ceux.
Et alors ? Si vous injectez de l'transactionManager dans votre bean et d'appel .commit() ce qui va mal ?
l'erreur sur le message d'origine---(Pas le droit de créer des transactions sur le partage de l'EntityManager - utiliser le Printemps des transactions ou des EJB CMT à la place)
OriginalL'auteur Gab
Le problème, c'est que votre méthode de sauvegarde est annoté Transactionnelle, ce qui signifie que la transaction doit être traitée par le Printemps, mais à l'intérieur de la méthode que vous essayez de gérer les transactions par programmation à l'aide de l'API JPA.
Si vous êtes à tort mélange deux approches. Idéalement, vous devriez utiliser uniquement des opérations d'annotation.
Ci-dessous, j'ai ajouté le code source à partir du Printemps classes d'aide qui gère cela. La classe concernée est org.springframework.orm.jpa.SharedEntityManagerCreator.Il dispose d'une salle de classe statique SharedEntityManagerInvocationHandler et le code où l'erreur est levée est inférieur à
C'est donc ce que j'ai mentionné ci-dessus.
Vous appelez getTransaction méthode et vous obtenez l'erreur.
Afin de se débarrasser de la gestion des transactions à l'intérieur de la méthode et de s'en tenir uniquement à l'affichage et de l'annotation.
boolean ok = SolrJ.IndexingWallPost(solrURL,userid, wallPost.getTitle(), wallPost.getMessage(), hashtag,groupsid); if(!ok) // throw some RuntimeException;
je suis d'accord avec vos points en fonction de mes recherches et de compréhension. je suppose que le printemps est occuper de la transaction et j'ai essayer de l'obtenir et de le contrôler. Mais je viens de supprimer le @Transactionnelle et essayer d'obtenir de transaction, puis j'ai rencontré ce problèmes ("java.lang.IllegalStateException: Pas autorisés à créer de la transaction sur le partage de l'EntityManager - utiliser le Printemps des transactions ou des EJB CMT à la place")
orid---votre méthode de travail, mais juste pour être sûr qu'aucun try et catch dans ce domaine, d'autre aura même des problèmes avec moi.merci!
OriginalL'auteur Shailendra