Connection.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.datasource;

  21. import java.sql.Array;
  22. import java.sql.Blob;
  23. import java.sql.CallableStatement;
  24. import java.sql.Clob;
  25. import java.sql.DatabaseMetaData;
  26. import java.sql.NClob;
  27. import java.sql.PreparedStatement;
  28. import java.sql.SQLClientInfoException;
  29. import java.sql.SQLException;
  30. import java.sql.SQLWarning;
  31. import java.sql.SQLXML;
  32. import java.sql.Savepoint;
  33. import java.sql.Statement;
  34. import java.sql.Struct;
  35. import java.util.Date;
  36. import java.util.Map;
  37. import java.util.Properties;
  38. import java.util.concurrent.Executor;

  39. import org.openspcoop2.utils.TipiDatabase;
  40. import org.openspcoop2.utils.UtilsException;
  41. import org.openspcoop2.utils.date.DateManager;
  42. import org.openspcoop2.utils.id.UUIDUtilsGenerator;
  43. import org.openspcoop2.utils.jdbc.JDBCUtilities;

  44. /**
  45.  * Connection
  46.  *  
  47.  * @author Poli Andrea (apoli@link.it)
  48.  * @author $Author$
  49.  * @version $Rev$, $Date$
  50.  */
  51. public class Connection implements java.sql.Connection {

  52.     /** Connessione Originale */
  53.     private java.sql.Connection wrappedConnection;
  54.    
  55.     /** Identificatore univoco risorsa */
  56.     private String id = null;
  57.    
  58.     /** Data di rilascio della risorsa */
  59.     private Date date = null;
  60.    
  61.     /** Modulo funzionale richiedente della risorsa */
  62.     private Object moduloFunzionale = null; // object in modo da poter fornire qualsiasi oggetto, anche una stringa
  63.    
  64.     /** IDTransazione */
  65.     private String idTransazione = null;
  66.    
  67.     /** Tipo di database */
  68.     private TipiDatabase tipoDatabase = null;
  69.    
  70.     /** Identificativo Datasource */
  71.     private String uuidDatasource = null;
  72.    
  73.     protected Connection(java.sql.Connection connection, TipiDatabase tipoDatabase, String idTransazione, Object moduloFunzionale,
  74.             String uuidDatasource) throws UtilsException{
  75.         this.wrappedConnection = connection;
  76.         this.date = DateManager.getDate();
  77.         this.moduloFunzionale = moduloFunzionale;
  78.         this.idTransazione = idTransazione;
  79.         this.tipoDatabase = tipoDatabase;
  80.         try{
  81.             this.id = UUIDUtilsGenerator.newUUID();
  82.         }catch(Exception e){
  83.             throw new UtilsException(e.getMessage(),e);
  84.         }
  85.         this.uuidDatasource = uuidDatasource;
  86.     }
  87.    
  88.    
  89.     public java.sql.Connection getWrappedConnection() {
  90.         return this.wrappedConnection;
  91.     }
  92.    
  93.     protected void closeWrappedConnection() throws SQLException{
  94.         if(!this.wrappedConnection.isClosed()){
  95.             JDBCUtilities.closeConnection(DataSource.checkLogger, this.wrappedConnection, DataSource.checkAutocommit, DataSource.checkIsClosed);
  96.         }
  97.     }
  98.    
  99.     @Override
  100.     public void close() throws SQLException {
  101.         try{
  102.             DataSourceFactory.getInstance(this.uuidDatasource).unregisterConnection(this);
  103.         }catch(Exception e){
  104.             throw new SQLException(e.getMessage(),e);
  105.         }
  106.     }
  107.    
  108.     @Override
  109.     public boolean isClosed() throws SQLException {
  110.         return this.wrappedConnection.isClosed();
  111.     }
  112.    
  113.     @Override
  114.     public void setTransactionIsolation(int level) throws SQLException {
  115.         if(java.sql.Connection.TRANSACTION_SERIALIZABLE == level){
  116.             JDBCUtilities.setTransactionIsolationSerializable(this.tipoDatabase, this.wrappedConnection);
  117.         }
  118.         else{
  119.             this.wrappedConnection.setTransactionIsolation(level);
  120.         }
  121.     }
  122.    
  123.     @Override
  124.     public int getTransactionIsolation() throws SQLException {
  125.         return this.wrappedConnection.getTransactionIsolation();
  126.     }
  127.    
  128.    
  129.    
  130.    
  131.     // **** GET ****
  132.    
  133.     public String getId() {
  134.         return this.id;
  135.     }

  136.     public Date getDate() {
  137.         return this.date;
  138.     }

  139.     public Object getModuloFunzionale() {
  140.         return this.moduloFunzionale;
  141.     }

  142.     public String getIdTransazione() {
  143.         return this.idTransazione;
  144.     }

  145.     public TipiDatabase getTipoDatabase() {
  146.         return this.tipoDatabase;
  147.     }
  148.    
  149.    
  150.    
  151.    
  152.     // **** METODI SU CUI E' STATO SOLAMENTE EFFETTUATO IL WRAP ******
  153.    
  154.     @Override
  155.     public <T> T unwrap(Class<T> iface) throws SQLException {
  156.         return this.wrappedConnection.unwrap(iface);
  157.     }

  158.     @Override
  159.     public boolean isWrapperFor(Class<?> iface) throws SQLException {
  160.         return this.wrappedConnection.isWrapperFor(iface);
  161.     }

  162.     @Override
  163.     public Statement createStatement() throws SQLException {
  164.         return this.wrappedConnection.createStatement();
  165.     }

  166.     @Override
  167.     public PreparedStatement prepareStatement(String sql) throws SQLException {
  168.         return this.wrappedConnection.prepareStatement(sql);
  169.     }

  170.     @Override
  171.     public CallableStatement prepareCall(String sql) throws SQLException {
  172.         return this.wrappedConnection.prepareCall(sql);
  173.     }

  174.     @Override
  175.     public String nativeSQL(String sql) throws SQLException {
  176.         return this.wrappedConnection.nativeSQL(sql);
  177.     }

  178.     @Override
  179.     public void setAutoCommit(boolean autoCommit) throws SQLException {
  180.         this.wrappedConnection.setAutoCommit(autoCommit);
  181.     }

  182.     @Override
  183.     public boolean getAutoCommit() throws SQLException {
  184.         return this.wrappedConnection.getAutoCommit();
  185.     }

  186.     @Override
  187.     public void commit() throws SQLException {
  188.         this.wrappedConnection.commit();
  189.     }

  190.     @Override
  191.     public void rollback() throws SQLException {
  192.         this.wrappedConnection.rollback();
  193.     }
  194.     @Override
  195.     public DatabaseMetaData getMetaData() throws SQLException {
  196.         return this.wrappedConnection.getMetaData();
  197.     }

  198.     @Override
  199.     public void setReadOnly(boolean readOnly) throws SQLException {
  200.         this.wrappedConnection.setReadOnly(readOnly);
  201.     }

  202.     @Override
  203.     public boolean isReadOnly() throws SQLException {
  204.         return this.wrappedConnection.isReadOnly();
  205.     }

  206.     @Override
  207.     public void setCatalog(String catalog) throws SQLException {
  208.         this.wrappedConnection.setCatalog(catalog);
  209.     }

  210.     @Override
  211.     public String getCatalog() throws SQLException {
  212.         return this.wrappedConnection.getCatalog();
  213.     }

  214.     @Override
  215.     public SQLWarning getWarnings() throws SQLException {
  216.         return this.wrappedConnection.getWarnings();
  217.     }

  218.     @Override
  219.     public void clearWarnings() throws SQLException {
  220.         this.wrappedConnection.clearWarnings();
  221.     }

  222.     @Override
  223.     public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
  224.         return this.wrappedConnection.createStatement(resultSetType, resultSetConcurrency);
  225.     }

  226.     @Override
  227.     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
  228.             throws SQLException {
  229.         return this.wrappedConnection.prepareStatement(sql, resultSetType, resultSetConcurrency);
  230.     }

  231.     @Override
  232.     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
  233.         return this.wrappedConnection.prepareCall(sql, resultSetType, resultSetConcurrency);
  234.     }

  235.     @Override
  236.     public Map<String, Class<?>> getTypeMap() throws SQLException {
  237.         return this.wrappedConnection.getTypeMap();
  238.     }

  239.     @Override
  240.     public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
  241.         this.wrappedConnection.setTypeMap(map);
  242.     }

  243.     @Override
  244.     public void setHoldability(int holdability) throws SQLException {
  245.         this.wrappedConnection.setHoldability(holdability);
  246.     }

  247.     @Override
  248.     public int getHoldability() throws SQLException {
  249.         return this.wrappedConnection.getHoldability();
  250.     }

  251.     @Override
  252.     public Savepoint setSavepoint() throws SQLException {
  253.         return this.wrappedConnection.setSavepoint();
  254.     }

  255.     @Override
  256.     public Savepoint setSavepoint(String name) throws SQLException {
  257.         return this.wrappedConnection.setSavepoint(name);
  258.     }

  259.     @Override
  260.     public void rollback(Savepoint savepoint) throws SQLException {
  261.         this.wrappedConnection.rollback(savepoint);
  262.     }

  263.     @Override
  264.     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
  265.         this.wrappedConnection.releaseSavepoint(savepoint);
  266.     }

  267.     @Override
  268.     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
  269.             throws SQLException {
  270.         return this.wrappedConnection.createStatement(resultSetType, resultSetConcurrency,resultSetHoldability);
  271.     }

  272.     @Override
  273.     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
  274.             int resultSetHoldability) throws SQLException {
  275.         return this.wrappedConnection.prepareStatement(sql, resultSetType, resultSetConcurrency,resultSetHoldability);
  276.     }

  277.     @Override
  278.     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
  279.             int resultSetHoldability) throws SQLException {
  280.         return this.wrappedConnection.prepareCall(sql, resultSetType, resultSetConcurrency,resultSetHoldability);
  281.     }

  282.     @Override
  283.     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
  284.         return this.wrappedConnection.prepareStatement(sql, autoGeneratedKeys);
  285.     }

  286.     @Override
  287.     public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
  288.         return this.wrappedConnection.prepareStatement(sql, columnIndexes);
  289.     }

  290.     @Override
  291.     public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
  292.         return this.wrappedConnection.prepareStatement(sql, columnNames);
  293.     }

  294.     @Override
  295.     public Clob createClob() throws SQLException {
  296.         return this.wrappedConnection.createClob();
  297.     }

  298.     @Override
  299.     public Blob createBlob() throws SQLException {
  300.         return this.wrappedConnection.createBlob();
  301.     }

  302.     @Override
  303.     public NClob createNClob() throws SQLException {
  304.         return this.wrappedConnection.createNClob();
  305.     }

  306.     @Override
  307.     public SQLXML createSQLXML() throws SQLException {
  308.         return this.wrappedConnection.createSQLXML();
  309.     }

  310.     @Override
  311.     public boolean isValid(int timeout) throws SQLException {
  312.         return this.wrappedConnection.isValid(timeout);
  313.     }

  314.     @Override
  315.     public void setClientInfo(String name, String value) throws SQLClientInfoException {
  316.         this.wrappedConnection.setClientInfo(name,value);
  317.     }

  318.     @Override
  319.     public void setClientInfo(Properties properties) throws SQLClientInfoException {
  320.         this.wrappedConnection.setClientInfo(properties);
  321.     }

  322.     @Override
  323.     public String getClientInfo(String name) throws SQLException {
  324.         return this.wrappedConnection.getClientInfo(name);
  325.     }

  326.     @Override
  327.     public Properties getClientInfo() throws SQLException {
  328.         return this.wrappedConnection.getClientInfo();
  329.     }

  330.     @Override
  331.     public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
  332.         return this.wrappedConnection.createArrayOf(typeName,elements);
  333.     }

  334.     @Override
  335.     public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
  336.         return this.wrappedConnection.createStruct(typeName,attributes);
  337.     }


  338.     @Override
  339.     public void setSchema(String schema) throws SQLException {
  340.         this.wrappedConnection.setSchema(schema);
  341.     }


  342.     @Override
  343.     public String getSchema() throws SQLException {
  344.         return this.wrappedConnection.getSchema();
  345.     }


  346.     @Override
  347.     public void abort(Executor executor) throws SQLException {
  348.         this.wrappedConnection.abort(executor);
  349.     }


  350.     @Override
  351.     public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
  352.         this.wrappedConnection.setNetworkTimeout(executor, milliseconds);
  353.     }


  354.     @Override
  355.     public int getNetworkTimeout() throws SQLException {
  356.         return this.wrappedConnection.getNetworkTimeout();
  357.     }

  358. }