All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yestech.lib.hibernate.client.ClientSession Maven / Gradle / Ivy

Go to download

A collection of classes that can be used across yestech artifacts/components, but must not be dependant on any yestech component. Most of the code is utility type code. When more than a few classes are found to be in a package or the package start to handle more that a few reposibilities then a new independant component is created and the existing code in yeslib is ported to the new component.

The newest version!
package org.yestech.lib.hibernate.client;

import org.springframework.context.ApplicationContext;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.rmi.RemoteException;


/**
 *
 *
 */
public class ClientSession
{
    private static final ThreadLocal REF = new ThreadLocal();
    private static ApplicationContext applicationContext;

    public static void init(ApplicationContext applicationContext)
    {
        ClientSession.applicationContext = applicationContext;
    }

    private static void start() throws ClassCastException, NamingException, RemoteException
    {
        if (isActive())
        {
            throw new IllegalStateException("nested sessions are not supported");
        }


        HibernateServerRemote home = (HibernateServerRemote) applicationContext.getBean("hibernateServer");

        REF.set(home);
    }

    private static boolean isActive()
    {
        return REF.get() != null;
    }

    /*package*/
    static HibernateServerRemote getLoader()
    {
        if (!isActive())
        {
            throw new IllegalStateException("session is not active");
        }
        return REF.get();
    }

    private static void release() throws RemoteException
    {
        HibernateServerRemote loader = REF.get();
        if (loader == null)
        {
            throw new IllegalStateException("session is not started");
        }
        REF.set(null);
        //loader.remove();
    }

    public static void execute(Runnable cmd) throws Exception
    {

        InitialContext context = new InitialContext();
        try
        {

            /*
            UserTransaction transaction = (UserTransaction)
                    context.lookup("UserTransaction");

            transaction.begin();
            */
            try
            {
                start();
                try
                {
                    cmd.run();
                }
                finally
                {
                    release();
                }
              //  transaction.commit();
            }
            finally
            {
                /*
                if (transaction.getStatus() == Status.STATUS_ACTIVE)
                {
                    transaction.rollback();
                }
                */
            }
        }
        finally
        {
            context.close();
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy