Semaphore.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.semaphore;

  21. import java.sql.Connection;

  22. import org.slf4j.Logger;
  23. import org.openspcoop2.utils.TipiDatabase;
  24. import org.openspcoop2.utils.UtilsException;
  25. import org.openspcoop2.utils.id.serial.InfoStatistics;
  26. import org.openspcoop2.utils.jdbc.JDBCUtilities;

  27. /**
  28.  * IDSerialGenerator
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class Semaphore {

  35.     private InfoStatistics infoStatistics;
  36.     private SemaphoreEngine engine;
  37.     private TipiDatabase databaseType;
  38.     private Logger log;
  39.     private boolean serializableLevel;
  40.     public Semaphore(InfoStatistics infoStatistics, SemaphoreMapping mapping, SemaphoreConfiguration config, TipiDatabase databaseType, Logger log) throws UtilsException{
  41.         this.infoStatistics = infoStatistics;
  42.         this.engine = new SemaphoreEngine(mapping, config, databaseType, log);
  43.         this.databaseType = databaseType;
  44.         this.log = log;
  45.         this.serializableLevel = config.isSerializableLevel();
  46.     }
  47.    
  48.     public boolean createEmptyLock(Connection conDB, boolean throwExceptionIfExists) throws UtilsException{
  49.         return this.engine.createEmptyLock(conDB, throwExceptionIfExists);  
  50.     }
  51.    
  52.    
  53.     public boolean newLock(Connection con,String details) throws UtilsException {
  54.         return this._engine(con, details, SemaphoreOperationType.NEW);
  55.     }
  56.     public boolean updateLock(Connection con,String details) throws UtilsException {
  57.         return this._engine(con, details, SemaphoreOperationType.UPDATE);
  58.     }
  59.     public boolean releaseLock(Connection con,String details) throws UtilsException {
  60.         return this._engine(con, details, SemaphoreOperationType.RELEASE);
  61.     }
  62.    
  63.     private boolean _engine(Connection con,String details,SemaphoreOperationType tipo) throws UtilsException {

  64.         // Check connessione
  65.         if(con == null){
  66.             throw new UtilsException("Connessione non fornita");
  67.         }
  68.         try{
  69.             if(con.isClosed()){
  70.                 throw new UtilsException("Connessione risulta giĆ  chiusa");
  71.             }
  72.         }catch(Exception e){
  73.             throw new UtilsException("Test Connessione non riuscito: "+e.getMessage(),e);
  74.         }

  75.         boolean originalConnectionAutocommit = false;
  76.         boolean autoCommitModificato = false;
  77.         try{
  78.             originalConnectionAutocommit = con.getAutoCommit();
  79.         }catch(Exception e){
  80.             throw new UtilsException("Verifica AutoCommit Connessione non riuscito: "+e.getMessage(),e);
  81.         }
  82.         if(originalConnectionAutocommit==false){
  83.             throw new UtilsException("Lock ["+tipo+"] failed (Non e' possibile fornire una connessione con autocommit disabilitato poiche' l'utility ha necessita' di effettuare operazioni di commit/rollback)");      
  84.         }
  85.        
  86.         int originalConnectionTransactionIsolation = -1;
  87.         boolean transactionIsolationModificato = false;
  88.         try{
  89.             originalConnectionTransactionIsolation = con.getTransactionIsolation();
  90.         }catch(Exception e){
  91.             throw new UtilsException("Lettura livello di isolamento transazione della Connessione non riuscito: "+e.getMessage(),e);
  92.         }
  93.        
  94.         try{        
  95.             try{                

  96.                 //System.out.println("SET TRANSACTION SERIALIZABLE ("+conDB.getTransactionIsolation()+","+conDB.getAutoCommit()+")");
  97.                 // Il rollback, non servirebbe, pero le WrappedConnection di JBoss hanno un bug, per cui alcune risorse non vengono rilasciate.
  98.                 // Con il rollback tali risorse vengono rilasciate, e poi effettivamente la ConnectionSottostante emette una eccezione.
  99.                 try{
  100.                     con.rollback();
  101.                 }catch(Exception e){
  102.                     //System.out.println("ROLLBACK ERROR: "+e.getMessage());
  103.                 }
  104.                
  105.                 if(this.serializableLevel) {
  106.                     JDBCUtilities.setTransactionIsolationSerializable(this.databaseType, con);
  107.                     transactionIsolationModificato = true;
  108.                 }
  109.                
  110.                 if(originalConnectionAutocommit){
  111.                     con.setAutoCommit(false);
  112.                     autoCommitModificato = true;
  113.                 }
  114.                
  115.             } catch(Exception er) {
  116.                 this.log.error("Lock ["+tipo+"] failed (impostazione transazione): "+er.getMessage(),er);
  117.                 throw new UtilsException("Lock ["+tipo+"] failed (impostazione transazione): "+er.getMessage(),er);    
  118.             }

  119.             return this.engine.lock(con, details, this.infoStatistics, tipo);  

  120.         }
  121.         finally{

  122.             // Ripristino Transazione
  123.             try{
  124.                 if(transactionIsolationModificato){
  125.                     con.setTransactionIsolation(originalConnectionTransactionIsolation);
  126.                 }
  127.                 if(autoCommitModificato){
  128.                     con.setAutoCommit(originalConnectionAutocommit);
  129.                 }
  130.             } catch(Exception er) {
  131.                 //System.out.println("ERROR UNSET:"+er.getMessage());
  132.                 this.log.error("Lock ["+tipo+"] failed (ripristino transazione): "+er.getMessage());
  133.                 //throw new UtilsException("Lock ["+tipo+"] failed (ripristino transazione): "+er.getMessage());
  134.             }
  135.         }

  136.     }



  137. }