public DB2OpenConnection GetOpenConnection(DB2Connection db2Conn) { DB2OpenConnection connection = null; lock (openFreeConnections.SyncRoot) { if ((connectionSettings.ConnectionPoolSizeMax > 0) && (connectionsOpen >= connectionSettings.ConnectionPoolSizeMax)) { throw new ArgumentException("Maximum connections reached for connectionstring"); } while (connectionsOpen > connectionsInUse) { connection = (DB2OpenConnection)openFreeConnections[openFreeConnections.Count - 1]; openFreeConnections.RemoveAt(openFreeConnections.Count - 1); // check if connection is dead int isDead; short sqlRet = DB2CLIWrapper.SQLGetConnectAttr(connection.DBHandle, DB2Constants.SQL_ATTR_CONNECTION_DEAD, out isDead, 0, IntPtr.Zero); if (((sqlRet == DB2Constants.SQL_SUCCESS_WITH_INFO) || (sqlRet == DB2Constants.SQL_SUCCESS)) && (isDead == DB2Constants.SQL_CD_FALSE)) { connectionsInUse++; break; } else { connectionsOpen--; connection.Dispose(); connection = null; } } if (connectionsOpen == connectionsInUse) { if (timer != null) { timer.Dispose(); timer = null; } } } if (connection == null) { openFreeConnections.Clear(); connectionsUsableOffset = 0; connection = new DB2OpenConnection(connectionSettings, db2Conn); connectionsOpen++; connectionsInUse++; } return(connection); }
public void AddToFreeConnections(DB2OpenConnection connection) { lock (openFreeConnections.SyncRoot) { connection.PoolDisposalTime = DateTime.Now.Add(connectionSettings.ConnectionLifeTime); if (timer == null) { timer = new Timer(new TimerCallback(DisposeTimedoutConnections), null, connectionSettings.ConnectionLifeTime, new TimeSpan(-1)); } connectionsInUse--; openFreeConnections.Add(connection); } }
unsafe public void Open() { if (disposed) { throw new ObjectDisposedException("DB2Connection"); } if (this.State == ConnectionState.Open || this.State == ConnectionState.Connecting || this.State == ConnectionState.Executing || this.State == ConnectionState.Fetching) { throw new InvalidOperationException("Connection already open"); } try { openConnection = connectionSettings.GetRealOpenConnection(this); } catch (DB2Exception) { Close(); throw; } }
public void Close() { DB2Transaction transaction = null; if (refTransaction != null) { transaction = (DB2Transaction)refTransaction.Target; } if ((transaction != null) && refTransaction.IsAlive) { transaction.Dispose(); } if (refCommands != null) { for (int i = 0; i < refCommands.Count; i++) { DB2Command command = null; if (refCommands[i] != null) { command = (DB2Command)((WeakReference)refCommands[i]).Target; } if ((command != null) && ((WeakReference)refCommands[i]).IsAlive) { try { command.ConnectionClosed(); } catch {} } //?? refCommands[i] = null; } } if (openConnection != null) { openConnection.Close(); openConnection = null; } }
//private void InternalOpen() //{ // try // { // DB2Constants.RetCode sqlRet = (DB2Constants.RetCode)DB2CLIWrapper.SQLAllocHandle(DB2Constants.SQL_HANDLE_DBC, DB2Environment.Instance.PenvHandle, out dbHandle); // DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, DB2Environment.Instance.PenvHandle, "Unable to allocate database handle in DB2Connection.", this); // StringBuilder outConnectStr = new StringBuilder(DB2Constants.SQL_MAX_OPTION_STRING_LENGTH); // short numOutCharsReturned; // sqlRet = (DB2Constants.RetCode)DB2CLIWrapper.SQLDriverConnect(dbHandle, IntPtr.Zero, // connectionString, DB2Constants.SQL_NTS, // outConnectStr, DB2Constants.SQL_MAX_OPTION_STRING_LENGTH, out numOutCharsReturned, // DB2Constants.SQL_DRIVER_NOPROMPT); // DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, dbHandle, "Unable to connect to the database.", this); // databaseProductName = SQLGetInfo(dbHandle, DB2Constants.SQL_DBMS_NAME); // databaseVersion = SQLGetInfo(dbHandle, DB2Constants.SQL_DBMS_VER); // /* Set the attribute SQL_ATTR_XML_DECLARATION to skip the XML declaration from XML Data */ // sqlRet = (DB2Constants.RetCode)DB2CLIWrapper.SQLSetConnectAttr(dbHandle, DB2Constants.SQL_ATTR_XML_DECLARATION, new IntPtr(DB2Constants.SQL_XML_DECLARATION_NONE), DB2Constants.SQL_NTS); // DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, dbHandle, "Unable to set SQL_ATTR_XML_DECLARATION", this); // nativeOpenPerformed = true; // } // catch // { // if (dbHandle != IntPtr.Zero) // { // DB2CLIWrapper.SQLFreeHandle(DB2Constants.SQL_HANDLE_DBC, dbHandle); // dbHandle = IntPtr.Zero; // } // throw; // } //} public override void Open() { if (disposed) { throw new ObjectDisposedException("DB2Connection"); } if (this.State == ConnectionState.Open) { throw new InvalidOperationException("Connection already open"); } try { //InternalOpen(); openConnection = connectionSettings.GetRealOpenConnection(this); } catch (DB2Exception) { Close(); throw; } }
unsafe public void Open() { if(disposed) { throw new ObjectDisposedException("DB2Connection"); } if (this.State == ConnectionState.Open || this.State == ConnectionState.Connecting || this.State == ConnectionState.Executing || this.State == ConnectionState.Fetching) { throw new InvalidOperationException("Connection already open"); } try { openConnection = connectionSettings.GetRealOpenConnection(this); } catch (DB2Exception) { Close(); throw; } }
public void Close() { DB2Transaction transaction = null; if(refTransaction != null) transaction = (DB2Transaction)refTransaction.Target; if((transaction != null) && refTransaction.IsAlive) { transaction.Dispose(); } if(refCommands != null) { for(int i = 0; i < refCommands.Count; i++) { DB2Command command = null; if(refCommands[i] != null) { command = (DB2Command)((WeakReference)refCommands[i]).Target; } if((command != null) && ((WeakReference)refCommands[i]).IsAlive) { try { command.ConnectionClosed(); } catch{} } //?? refCommands[i] = null; } } if(openConnection != null) { openConnection.Close(); openConnection = null; } }
public DB2OpenConnection GetOpenConnection(DB2Connection db2Conn) { DB2OpenConnection connection = null; lock(openFreeConnections.SyncRoot) { if((connectionSettings.ConnectionPoolSizeMax > 0) && (connectionsOpen >= connectionSettings.ConnectionPoolSizeMax)) { throw new ArgumentException("Maximum connections reached for connectionstring"); } while(connectionsOpen > connectionsInUse) { connection = (DB2OpenConnection)openFreeConnections[openFreeConnections.Count - 1]; openFreeConnections.RemoveAt(openFreeConnections.Count - 1); // check if connection is dead int isDead; short sqlRet = DB2CLIWrapper.SQLGetConnectAttr(connection.DBHandle, DB2Constants.SQL_ATTR_CONNECTION_DEAD, out isDead, 0, IntPtr.Zero); if(((sqlRet == DB2Constants.SQL_SUCCESS_WITH_INFO) || (sqlRet == DB2Constants.SQL_SUCCESS)) && (isDead == DB2Constants.SQL_CD_FALSE)) { connectionsInUse++; break; } else { connectionsOpen--; connection.Dispose(); connection = null; } } if(connectionsOpen == connectionsInUse) { if(timer != null) { timer.Dispose(); timer = null; } } } if(connection == null) { openFreeConnections.Clear(); connectionsUsableOffset = 0; connection = new DB2OpenConnection(connectionSettings, db2Conn); connectionsOpen++; connectionsInUse++; } return connection; }
public void AddToFreeConnections(DB2OpenConnection connection) { lock(openFreeConnections.SyncRoot) { connection.poolDisposalTime = DateTime.Now.Add(connectionSettings.ConnectionLifeTime); if(timer == null) { timer = new Timer(new TimerCallback(DisposeTimedoutConnections), null, connectionSettings.ConnectionLifeTime, new TimeSpan(-1)); } connectionsInUse--; openFreeConnections.Add(connection); } }