⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbpersistenceservice.java

📁 workflow first jbpm
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.persistence.db;

import java.sql.Connection;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;
import org.hibernate.Transaction;
import org.jbpm.JbpmContext;
import org.jbpm.JbpmException;
import org.jbpm.db.ContextSession;
import org.jbpm.db.GraphSession;
import org.jbpm.db.JobSession;
import org.jbpm.db.LoggingSession;
import org.jbpm.db.TaskMgmtSession;
import org.jbpm.persistence.JbpmPersistenceException;
import org.jbpm.persistence.PersistenceService;
import org.jbpm.svc.Service;
import org.jbpm.svc.Services;
import org.jbpm.tx.TxService;

public class DbPersistenceService implements Service, PersistenceService {
  
  private static final long serialVersionUID = 1L;

  protected DbPersistenceServiceFactory persistenceServiceFactory = null;

  protected Connection connection = null;
  protected boolean mustConnectionBeClosed = false;

  protected Transaction transaction = null;
  protected boolean isTransactionEnabled = true;
  protected boolean isCurrentSessionEnabled = false;

  // boolean isRollbackOnly = false;

  protected Session session;
  protected boolean mustSessionBeFlushed = false;
  protected boolean mustSessionBeClosed = false;

  protected Services services = null;

  protected GraphSession graphSession = null;
  protected TaskMgmtSession taskMgmtSession = null;
  protected JobSession jobSession = null;
  protected ContextSession contextSession = null;
  protected LoggingSession loggingSession = null;

  public DbPersistenceService(DbPersistenceServiceFactory persistenceServiceFactory) {
    this(persistenceServiceFactory, getCurrentServices());
  }

  static Services getCurrentServices() {
    Services services = null;
    JbpmContext currentJbpmContext = JbpmContext.getCurrentJbpmContext();
    if (currentJbpmContext!=null) {
      services = currentJbpmContext.getServices();
    }
    return services;
  }

  DbPersistenceService(DbPersistenceServiceFactory persistenceServiceFactory, Services services) {
    this.persistenceServiceFactory = persistenceServiceFactory;
    this.isTransactionEnabled = persistenceServiceFactory.isTransactionEnabled();
    this.isCurrentSessionEnabled = persistenceServiceFactory.isCurrentSessionEnabled();
    this.services = services;
  }

  public SessionFactory getSessionFactory() {
    return persistenceServiceFactory.getSessionFactory();
  }

  public Session getSession() {
    if ( (session==null)
         && (getSessionFactory()!=null) 
       ) {
      Connection connection = getConnection(false);
      
      if (isCurrentSessionEnabled) {
//        session = getSessionFactory().getCurrentSession();
//        log.debug("using current hibernate session " + session);
//        mustSessionBeClosed = false;
//        mustSessionBeFlushed = false;
//        mustConnectionBeClosed = false;
//      } else if (connection!=null) {
//        log.debug("creating hibernate session with connection "+connection);
//        session = getSessionFactory().openSession(connection);
//        mustSessionBeClosed = true;
//        mustSessionBeFlushed = true;
//        mustConnectionBeClosed = false;
//      } else {
//        log.debug("creating hibernate session");
//        session = getSessionFactory().openSession();
//        mustSessionBeClosed = true;
//        mustSessionBeFlushed = true;
//        mustConnectionBeClosed = false;
//      }
//      log.debug("creating hibernate session");
	    session = getSessionFactory().openSession();
	    mustSessionBeClosed = true;
	    mustSessionBeFlushed = true;
	    mustConnectionBeClosed = false;
	  }
      if (isTransactionEnabled) {
        beginTransaction();
      }
    }
      
    return session;
  }

  public void beginTransaction() {
    log.debug("beginning hibernate transaction");
    transaction = session.beginTransaction();
    log.debug("begun hibernate transaction " + transaction.toString());
  }

  public void endTransaction() {
    if ( (isTransactionEnabled)
         && (transaction!=null) 
       ) {
      if (isRollbackOnly()) {
        try {
          log.debug("rolling back hibernate transaction " + transaction.toString());
          mustSessionBeFlushed = false; // flushing updates that will be rolled back is not very clever :-) 
          transaction.rollback();
        } catch (Exception e) {
          // NOTE that Error's are not caught because that might halt the JVM and mask the original Error.
          throw new JbpmPersistenceException("couldn't rollback hibernate session", e);
        }
      } else {
        try {
          log.debug("committing hibernate transaction " + transaction.toString());
          mustSessionBeFlushed = false; // commit does a flush anyway 
          transaction.commit();
        } catch (Exception e) {
          // NOTE that Error's are not caught because that might halt the JVM and mask the original Error.
          try {
            // if the commit fails, we must do a rollback
            transaction.rollback();
          } catch (Exception e2) {
            // if the rollback fails, we did what we could and you're in 
            // deep shit :-(
            log.error("problem rolling back after failed commit", e2);
          }
          throw new JbpmPersistenceException("couldn't commit hibernate session", e);
        }
      }
    }
  }

  public Connection getConnection() {
    return getConnection(true);
  }

  public Connection getConnection(boolean resolveSession) {
    if (connection==null) {
      if (persistenceServiceFactory.getDataSource()!=null) { 
        try {
          log.debug("fetching jdbc connection from datasource");
          connection = persistenceServiceFactory.getDataSource().getConnection();
          mustConnectionBeClosed = true;
        } catch (Exception e) {
          // NOTE that Error's are not caught because that might halt the JVM and mask the original Error.
          throw new JbpmException("couldn't obtain connection from datasource", e);
        }
      } else {
        if (resolveSession) {
          // initializes the session member
          getSession();
        }
        if (session!=null) {
          connection = session.connection();
          log.debug("fetching connection from hibernate session. this transfers responsibility for closing the jdbc connection to the user! "+connection);
          mustConnectionBeClosed = false;
        }
      }
    }
    return connection;
  }
  
  public void close() {

    if ( (session!=null)
         && (transaction==null)
         && (isRollbackOnly())
       ) {
      throw new JbpmException("setRollbackOnly was invoked while configuration specifies user managed transactions");
    }
    
    if ( (isTransactionEnabled)
         && (transaction!=null) 
       ) {

      if (! isRollbackOnly()) {
        Exception commitException = commit();
        if (commitException!=null) {
          rollback();
          closeSession();
          closeConnection();
          throw new JbpmPersistenceException("hibernate commit failed", commitException);
        }

      } else { // isRollbackOnly==true
        Exception rollbackException = rollback();
        if (rollbackException!=null) {
          closeSession();
          closeConnection();
          throw new JbpmPersistenceException("hibernate rollback failed", rollbackException);
        }
      }

⌨️ 快捷键说明

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