Announcement Announcement Module
Collapse
No announcement yet.
LazyInitializationException in Thread Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • LazyInitializationException in Thread

    Hi,


    I made a Thread class (extends from Thread) because I need call to async operations. But when I tried to get this, for example a property from persitence class occurs a exception. This only occurs when I call from Thread class...

    I use Roo 1.5.

    My class is:

    Code:
    @RooJavaBean
    @RooToString
    @RooEntity
    public class Consulta {
        @OneToMany(cascade=CascadeType.ALL)
        private List<DetalleConsulta> detalleConsulta;
    }
    Code:
    /**
     * @author HDiaz
     *
     */
    public class ThreadIngresarConsulta extends Thread {
        
        private Long idConsulta;
        
        public ThreadIngresarConsultaCRM(Long idConsulta) {
            super("ThreadIngresarConsultaCRM");
            this.idConsulta = idConsulta;
        }
    
    
        public void run(){
            try {
                ThreadIngresarConsultaCRM.sleep(5000);
                Consulta consulta = Consulta.findConsulta(idConsulta);
                List<DetalleConsulta> lista = consulta.getDetalleConsulta();
    
    
    
            }catch(Exception e) {
                System.err.println(e.getMessage());
            }        
        }
    }
    And exception is:

    Code:
    2011-12-05 18:49:10,015 [ThreadIngresarConsulta] ERROR org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.core.Consulta.detalleConsulta, no session or session was closed
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.core.Consulta.detalleConsulta, no session or session was closed

  • #2
    I think the problem is that my main thread finishes before the ThreadIngresarConsulta.

    Any idea?

    Comment


    • #3
      Has anyone tried to call an entity JPA within a thread?

      Comment


      • #4
        Thanks to gkamalless from stackoverflow for his quickly help

        Solution:

        Try putting an @Transactional annotation on the run method of your thread. If that doesn't work move the two lines into a separate method and add @Transactional on that method.

        public class ThreadIngresarConsulta extends Thread {

        public void run(){
        doProcess();
        }

        @Transactional
        public void doProcess() {
        try {
        Consulta consulta = Consulta.findConsulta(idConsulta);
        List<DetalleConsulta> lista = consulta.getDetalleConsulta();
        }catch(Exception e) {
        System.err.println(e.getMessage());
        }
        }
        }
        http://stackoverflow.com/questions/8...spring-roo-1-5

        Comment

        Working...
        X