0

I have some problems saving entries to postgres DB. I am using hibernate and spring but I am unable to use the DAO class for this purpose. Instead the following code works for me:

Session session = sessionFactory.openSession();
    Transaction tx = null;
    Blob blob = null;

    try {
        tx = session.beginTransaction();
        VoiceMail message02 = new VoiceMail(44, 1, "dir/msgs",
                "dummy_data", "dummy_data", "6003", "19/01/2015",
                "1:35", "flag", "dummy_data", "dummy_data", blob,
                "label", false, "1");
        //voiceMailDao.save(message02);
        session.save(message02);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

When I try to comment the line

session.save(message02);

and leave the line

voiceMailDao.save(message02);

It doesn`t work. The following output is generated:

save is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy750.save(Unknown Source)
at com.musala.ving.daoimpl.VoiceMailDaoImpl.save(VoiceMailDaoImpl.java:17)
at com.musala.ving.controllers.VoiceMailController.list(VoiceMailController.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

Sorry for the long post but I read all of the topics regarding this problem here and I still cannot figure it out.

My DAO class is as follows:

public class VoiceMailDaoImpl  implements VoiceMailDao{

@Autowired
private SessionFactory sessionFactory;

public void save(VoiceMail voicemail) {
    this.sessionFactory.getCurrentSession().save(voicemail);
}

public void update(VoiceMail voicemail) {
    this.sessionFactory.getCurrentSession().update(voicemail);

}

public void delete(VoiceMail voicemail) {
    this.sessionFactory.getCurrentSession().delete(voicemail);

}

}

Any help is appreciated!

Thank you!

skywalker
  • 696
  • 2
  • 16
  • 37
  • This question really has been answered numerous times before on stack overflow. The answer is in those questions compare your configuration with the working ones from the answers. Judging from the stack trace you are messing around with the `hibernate.current_session_context` property. – M. Deinum Jan 23 '15 at 14:43
  • Thank you very much for the information! I managed to solve it using: `public void save(VoiceMail voicemail) { Session session=getSessionFactory().getCurrentSession(); Transaction trans=session.beginTransaction(); this.sessionFactory.getCurrentSession().save(voicemail); trans.commit(); }` – skywalker Jan 23 '15 at 15:03
  • That isn't the proper solution (as also stated in that answer and later on in that answer). – M. Deinum Jan 23 '15 at 15:04
  • ok, my property is: `thread` should I change it to something else? Thank you for your answer! – skywalker Jan 23 '15 at 15:08

1 Answers1

0

Can you just use annotations?

Its much easier, just annotate your dao class with Transactional.

@Transactional

Then all methods are automatically transactional, if you only want it for specific methods you can also just add the same annotation to the methods them-self. It takes the hassle of transaction handling.

See this link for a good sample on how to achieve this.

---- EDIT ----

Also if you are early enough into you project have a look at spring-boot, it will speed your system development up a lot. Spring - Boot. This project helps setup your system with some best practices and gets you going a lot faster. We are currently using this on new projects and it makes setting up new projects very fast.

joey.enfield
  • 1,229
  • 8
  • 14
  • I will try it. Thank you! But I created many classes and I don`t want to start from scratch implementing this. I just have to find a way for saving entries with my DAO class. – skywalker Jan 23 '15 at 14:25