Results 1 to 4 of 4

Thread: LazyInitializationException in Thread

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    21

    Question 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. #2
    Join Date
    Jun 2011
    Posts
    21

    Default

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

    Any idea?

  3. #3
    Join Date
    Jun 2011
    Posts
    21

    Default

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

  4. #4
    Join Date
    Jun 2011
    Posts
    21

    Default

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •