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

com.jfireframework.jfireSqlStarter.AutoSession Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
package com.jfireframework.jfireSqlStarter;

import java.sql.Connection;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jfireframework.jfire.support.BeanInstanceResolver.extend.aop.tx.RessourceManager;
import com.jfireframework.sql.dao.LockMode;
import com.jfireframework.sql.page.Page;
import com.jfireframework.sql.resultsettransfer.ResultSetTransfer;
import com.jfireframework.sql.session.SessionFactory;
import com.jfireframework.sql.session.SqlSession;

public class AutoSession implements RessourceManager
{
    private SessionFactory sessionFactory;
    private static Logger  logger = LoggerFactory.getLogger(AutoSession.class);
    
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
    
    @Override
    public void close()
    {
        logger.trace("关闭当前session{}", sessionFactory.getCurrentSession());
        SqlSession session = sessionFactory.getCurrentSession();
        if (session != null)
        {
            ((AutoSessionProxy) session).decr();
        }
    }
    
    @Override
    public void open()
    {
        SqlSession session = sessionFactory.getOrCreateCurrentSession();
        AutoSessionProxy proxy;
        if (session instanceof AutoSessionProxy)
        {
            proxy = (AutoSessionProxy) session;
        }
        else
        {
            proxy = new AutoSessionProxy(session);
            SessionFactory.CURRENT_SESSION.set(proxy);
        }
        proxy.incr();
    }
    
    class AutoSessionProxy implements SqlSession
    {
        final SqlSession host;
        int              count = 0;
        
        public void incr()
        {
            count += 1;
        }
        
        public void decr()
        {
            count -= 1;
            if (count == 0)
            {
                SessionFactory.CURRENT_SESSION.set(host);
                host.close();
            }
        }
        
        public AutoSessionProxy(SqlSession session)
        {
            this.host = session;
        }
        
        @Override
        public  T get(Class entityClass, Object pk)
        {
            return host.get(entityClass, pk);
        }
        
        @Override
        public  T get(Class entityClass, Object pk, LockMode mode)
        {
            return host.get(entityClass, pk, mode);
        }
        
        @Override
        public  void save(T entity)
        {
            host.save(entity);
        }
        
        @Override
        public  void batchInsert(List entitys)
        {
            host.batchInsert(entitys);
        }
        
        @Override
        public  void insert(T entity)
        {
            host.insert(entity);
        }
        
        @Override
        public  int delete(T entity)
        {
            return host.delete(entity);
        }
        
        @Override
        public  T query(ResultSetTransfer transfer, String sql, Object... params)
        {
            return host.query(transfer, sql, params);
        }
        
        @Override
        public  List queryList(ResultSetTransfer transfer, String sql, Object... params)
        {
            return host.queryList(transfer, sql, params);
        }
        
        @Override
        public  List queryList(ResultSetTransfer transfer, String sql, Page page, Object... params)
        {
            return host.queryList(transfer, sql, page, params);
        }
        
        @Override
        public int update(String sql, Object... params)
        {
            return host.update(sql, params);
        }
        
        @Override
        public void close()
        {
            ;
        }
        
        @Override
        public void beginTransAction(int isolate)
        {
            host.beginTransAction(isolate);
        }
        
        @Override
        public void commit()
        {
            host.commit();
        }
        
        @Override
        public void flush()
        {
            host.flush();
        }
        
        @Override
        public void rollback()
        {
            host.rollback();
        }
        
        @Override
        public Connection getConnection()
        {
            return host.getConnection();
        }
        
        @Override
        public  T findOne(Class entityClass, String strategy, Object... params)
        {
            return host.findOne(entityClass, strategy, params);
        }
        
        @Override
        public  List findAll(Class entityClass, String strategy, Object... params)
        {
            return host.findAll(entityClass, strategy, params);
        }
        
        @Override
        public  List findPage(Class entityClass, Page page, String strategy, Object... params)
        {
            return host.findPage(entityClass, page, strategy, params);
        }
        
        @Override
        public int update(Class ckass, String sql, Object... params)
        {
            return host.update(ckass, sql, params);
        }
        
        @Override
        public int delete(Class ckass, String strategy, Object... params)
        {
            return host.delete(ckass, strategy, params);
        }
        
        @Override
        public int count(Class ckass, String strategy, Object... params)
        {
            return host.count(ckass, strategy, params);
        }
        
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy