sessionmanager.java

来自「图书借阅系统」· Java 代码 · 共 118 行

JAVA
118
字号
// Decompiled by DJ v2.9.9.60 Copyright 2000 Atanas Neshkov  Date: 2004-12-7 21:22:46
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name:   SessionManager.java

package com.lib.dao;

import java.io.PrintStream;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;

public class SessionManager
{

    private SessionManager()
    {
        try
        {
            if(sf == null)
                synchronized(lockObj)
                {
                    if(sf == null)
                        sf = (new Configuration()).configure().buildSessionFactory();
                }
        }
        catch(HibernateException ex)
        {
            ex.printStackTrace();
        }
    }

    public static SessionManager getInstance()
    {
        return new SessionManager();
    }

    public Session getSession()
    {
        Session ss = null;
        try
        {
            ss = sf.openSession();
        }
        catch(HibernateException ex)
        {
            ex.printStackTrace();
        }
        return ss;
    }

    public Session openSession()
    {
        Session ss = (Session)currentThread.get();
        if(ss == null)
        {
            try
            {
                ss = sf.openSession();
            }
            catch(HibernateException ex)
            {
                ex.printStackTrace();
            }
            currentThread.set(ss);
        }
        if(!ss.isConnected() || !ss.isOpen())
            try
            {
                ss.reconnect();
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("=======================================");
                System.out.println("Session have been closed");
                System.out.println("Session have been opened now!");
            }
            catch(HibernateException ex1)
            {
                System.out.println("Session have been closed");
                ex1.printStackTrace();
            }
        return ss;
    }

    public void closeSession()
    {
        Session ss = (Session)currentThread.get();
        currentThread.set(null);
        try
        {
            if(ss != null)
                ss.close();
        }
        catch(HibernateException ex)
        {
            ex.printStackTrace();
        }
    }

    private static SessionFactory sf = null;
    private static final ThreadLocal currentThread = new ThreadLocal();
    private static Object lockObj = new Object();

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?