-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate + threads
PostPosted: Thu Oct 30, 2014 11:15 am 
Newbie

Joined: Thu Oct 30, 2014 10:47 am
Posts: 2
Hello,
How should I use hibernate properly from different threads ?

Main: (I'm getting new hibernateUtils and new authService here)
Code:
public class MainClass {
    private static AuthService authService;

    public static void main(String[] args) {
        try (HibernateUtils hibernateUtils = new HibernateUtils()) {
            init(hibernateUtils);
            UserInterface userInterface = new SwingInterface(authService);
            userInterface.run();
        } finally {
        }
    }

    private static void init(HibernateUtils hibernateUtils) {
        AuthDAO authDAO = new HibernateAuthDAOImpl(hibernateUtils);
        authService = new AuthServiceImpl(authDAO);
    }
}


HibernateUtils:
Code:
public class HibernateUtils implements AutoCloseable {
    private SessionFactory sessionFactory;
    private Session session;

    public HibernateUtils() {

        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
                applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory(builder.build());
    }

    public Session getSession() {
        if(session == null) {
            session = sessionFactory.openSession();
        }
        return session;
    }

    @Override
    public void close() {
         sessionFactory.close();
    }
}


SwingInterface, that makes 2nd thread
Code:
public class SwingInterface implements UserInterface {
    private AuthService authService;

    public SwingInterface(AuthService authService) {
        this.authService = authService;
    }

    @Override
    public void run() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AuthWindow(authService);
            }
        });
    }
}


SwingInterface makes 2nd thread for a window and if I call authService with hibernateUtils (that I've made in Main) I will get exception:
Code:
Exception in thread "AWT-EventQueue-0" org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
    at [...].db.utils.HibernateUtils.getSession(HibernateUtils.java:24)


But if I create new hibernateUtils in that new thread - all goes well.
What should I do ? How to do it correctly... ? Thank you.


Top
 Profile  
 
 Post subject: Re: Hibernate + threads
PostPosted: Sun Nov 02, 2014 6:50 pm 
Newbie

Joined: Thu Oct 30, 2014 10:47 am
Posts: 2
Moving
Code:
try (HibernateUtils hibernateUtils = new HibernateUtils()) {

from try to private static has solved my problem :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.