-->
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: problem with Hibernate in executable jar
PostPosted: Sun Nov 21, 2010 7:24 pm 
Beginner
Beginner

Joined: Tue Aug 03, 2010 4:32 pm
Posts: 22
Greetings all,

I'm attempting to create an executable jar for a standalone Java/Swing/Hibernate/MySQL app built in Eclipse/Galileo.
Everything works fine when I execute the app from within Eclipse.
I create a jarfile using the advanced features thereby creating the following ./src/manifest.mf :

Code:
Manifest-Version: 1.0
Main-Class: toad.model.TopLevelDemo


I've remembered to embed a single blank line as the last line of the file.

I then create an executable jar, clicking the "extract required libraries into generated jar" option. The resulting .jar is roughly 24Mbytes.

Double-clicking the executable jar does fire up the app and it does execute but stalls when it hits
Code:
        Session session_a = HibernateUtil.currentSession("toad");



Here's my HibernateUtil.java
Code:
package toad.persistence;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.apache.commons.logging.*;

import javax.naming.*;
import java.io.*;
import java.util.*;
import java.net.URL;

public class HibernateUtil
{
    private static final boolean usingAnnotations = true;
    private static Log log = LogFactory.getLog(HibernateUtil.class);
    private static HashMap<String, SessionFactory> sessionFactoryMap = new HashMap<String, SessionFactory>();
    public static final ThreadLocal sessionMapsThreadLocal = new ThreadLocal();

    static {
        try {
            String fileName;
            SessionFactory sf;
            // Create the SessionFactory objects from *.cfg.xml
            ArrayList<String> cfglist = getHibernateCfgFiles("/");
            for( String keyy : cfglist) {
                fileName = keyy + ".cfg.xml";
                if( usingAnnotations) {
                    sf = new AnnotationConfiguration().configure(fileName).buildSessionFactory();
                } else {
                    sf = new Configuration().configure(fileName).buildSessionFactory();                   
                }
                sessionFactoryMap.put(keyy, sf);
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
            log.error("Initial SessionFactory creation failed.", ex);
            System.out.println("Initial SessionFactories creation failed.");
            throw new ExceptionInInitializerError(ex);
   
        } // end of the try - catch block
    }
   
    public static Session currentSession() throws HibernateException
    {
        return currentSession("");
    }

    public static Session currentSession( String keyy) throws HibernateException
    {
        HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();

        if( sessionMaps == null) {
            sessionMaps = new HashMap();
            sessionMapsThreadLocal.set( sessionMaps);
        }

        // Open a new Session, if this Thread has none yet
        Session s = (Session) sessionMaps.get(keyy);
        if( s == null) {
            s = ((SessionFactory) sessionFactoryMap.get(keyy)).openSession();
            sessionMaps.put(keyy, s);
        }

        return s;
    }

    public static ArrayList<String> getHibernateCfgFiles( String path)
    {
        ArrayList<String> ret_arry = new ArrayList<String>();
        String fileName, keyy;
        String[] str_arry;

        URL url = Configuration.class.getResource( path);  // returns C:/eclipse_workspace/sais_check_a/bin
        File file = new File( url.getFile());
        if( file.isDirectory()) {
            for( File f : file.listFiles()) {
                if( f.isFile()) {
                    fileName = f.getName();
                    if( fileName.endsWith(".cfg.xml")) {
                        str_arry = fileName.split("\\.");
                        keyy = str_arry[0];
                        ret_arry.add( keyy);
                    }
                }
            }
        }
     
        return ret_arry;
       
    }

    public static void closeSession()
    {
        log.debug("closing a single session");
        HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();
        sessionMapsThreadLocal.set(null);
        if( sessionMaps != null) {
            Session session = sessionMaps.get("");
            if( session != null && session.isOpen())
                session.close();
        }
    }

    public static void closeSessions() throws HibernateException
    {
        log.debug("closing sessions");
        HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();
        sessionMapsThreadLocal.set(null);
        if( sessionMaps != null) {
            for( Session session : sessionMaps.values()) {
                if( session.isOpen())
                    session.close();
            };
        }
    }
   
    /**
     * Closes the current SessionFactory and releases all resources.
     * <p>
     * The only other method that can be called on HibernateUtil
     * after this one is rebuildSessionFactory(Configuration).
     */
    public static void shutdown()
    {
        log.debug("Shutting down Hibernate");
        closeSessions();
        // Close caches and connection pools
        if( sessionFactoryMap != null) {
            for( SessionFactory sf : sessionFactoryMap.values()) {
                 sf.close();
            }
        }
        sessionFactoryMap = null;  // Clear static variables
    }

}


What do I need to add to do to get the executable jarfile to work with Hibernate?

TIA,

Still-learning Stuart


Top
 Profile  
 
 Post subject: Re: problem with Hibernate in executable jar
PostPosted: Mon Nov 22, 2010 1:09 am 
Beginner
Beginner

Joined: Tue Aug 03, 2010 4:32 pm
Posts: 22
turns out the app stalls when it hits this line within the getHibernateCfgFiles function within HibernateUtil.java

Code:
        URL url = Configuration.class.getResource( path);  // returns C:/eclipse_workspace/sais_check_a/bin


turns out the line returns C:/eclipse_workspace/sais_check_a/bin when the app is run from within Eclipse
but returns a null when the app is run from an executable jar, so throws a NPE.

So what is/isnt going on here and how can I work arould this?

Still-learning Stuart


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.