ASP .Net Connection Pooling With Oracle - c#

Hello all respective genius there, thanks in advance.
I have an Asp.NET Web service which have around 20 to 30 methods to expose .
For database I am using Oracle database with the help of Delete repeated wordNet and a single class for the DB Operation and for every request I open the connection and close once its job get done for sure , means that I am closing all the connection there is no connection leak in my code. I have checked this like 10 times but as I go and check in the database (Oracle 11G) there are many session are inactive more than 20 hours . I am looking these sessions in V$Session table but I am very confused how they are not getting destroyed after long time even I am closing all the connections .
Please share your answers with me because all I can do close all the connection and that I am doing very well but still there are many inactive session more than 20 hours . How is this possible ?

We had similar problem in asp.net mvc project with connecting to Oracle Database (with pooling).
After a long time we found out the problem was with oracle db-links. If you calling a stored procedure which uses oracle db-link then you have to call DBMS_SESSION.CLOSE_DATABASE_LINK('db_link_name'); at the beginning of stored procedure.
For example:
PROCEDURE simple_procedure(refCur out SYS_REFCURSOR) AS BEGIN
/* we don't really know if db-link is open or not */
BEGIN
DBMS_SESSION.CLOSE_DATABASE_LINK('dblink_name');
EXCEPTION WHEN OTHERS THEN null;
END;
/* call db-link */
OPEN refCur FOR SELECT * FROM DUAL#dblink_name
EXCEPTION WHEN NO_DATA_FOUND THEN null;
END simple_procedure;
After this problems with session has gone.

Related

Frequent Opening and Closing of MySQL Database over Cloud

I have a local server MS-SQL situated in one of our branch. I want to transfer new data from the local server to MySql table (over the cloud) every 1 minute.
I have coded a small C# application which opens both the server connections, search and insert the new rows into MySql Database and then close the connection.
Now my question is, as I am continously opening, updating and closing the Mysql connection every minute; will there be any issues? Is there any other alternate method through which I can establish a single connection to MySql database and then keep on inserting the new rows every minute.
I appreciate your valuable support.
Below is the coding part I use to open, update and close the connection: Remember, the below method is executed every minute.
if (queryValues != "")
{
queryValues = queryValues.Remove(queryValues.Length - 1);
query = query + queryValues + ")";
MyCommand3 = new MySqlCommand(query, MyConn3);
if (MyConn3.State == 0)
MyConn3.Open();
MyReader3 = null;
MyReader3 = MyCommand3.ExecuteReader();
MyCommand3.Dispose();
MyReader3.Dispose();
MyConn3.Close();
}
It is not a problem.
Connector/NET, the MySQL driver for C#, offers connection pooling by default. That is, it keeps the connection between your program and MySQL open, so you can re-use it with another .Open() operation.
It only closes the connections when they get to be three minutes old, so you should be fine with a once-per-minute operation. And, it manages stuff like lost connections pretty well, so you don't have to.
What's more, opening a connection isn't a high-overhead operation. Only if you do it multiple times a second does the overhead get out of hand.

Open a sage 300 AccPac DBLink Connection to a session

I am trying to write c# code around opening a Sage 300 Connection using C#. I am using the Acccpac.Advantage DLL.
Here is my code
try
{
sage300Session.Init(sessionHandle, appID, programName, appVersion);
sage300Session.Open(_user, _ppswd, _companyID, DateTime.Today, 0);
// Open a database link.
sage300DbLink = sage300Session.OpenDBLink(DBLinkType.Company, DBLinkFlags.ReadWrite);
}
The issue I am having is, no matter what I put in the password, the call to .Open seems to succeed. If I put an invalid user or companyID, I get errors as expected. (the connestion status seems to say open either way).
My question is - what is happening with the password that is doesn't seem to be used and 2- when I am through with what I am doing, is there a way to correctly close the connection?
The Accpac.Advantage dll is v 2.0.50727 and I am connecting to Sage 300 2014 environment.
As it turned out, the security setting was not enabled in the system database to require passwords to log in. Setting that "resolved" the issue and made the password be used. I never did find a way to disconnect from the session so I let it disconnect when I am done with the processing by having the connection go out of scope
Actually, both Session and DBLink implement IDisposable and calling .Dispose (or the using keyword) would be enough to end the session. (I would have wanted to add this as a comment, but couldn't).

Simulate ORA-3113: end-of-file on communication channel

I have a C# Windows IIS server (Windows Server 2003) application connecting to an Oracle database hosted on Linux (10gR2 on Red Hat 5.3). Intermittetly, Oracle throws an ORA-3113: end-of-file on communication channel error. This screws up the OracleConnection object in C#. Then, any new OracleCommands that try to use the OracleConnection all fail saying the connection has been closed.
I have reviewed the Oracle trace files generated by this error and have isolated the problem to faulty network hardware and am working to fix it.
However, I need to make my C# code more robust and have it respond appropriately to this error by closing and not using that connection object anymore. It is easy to catch the exception in C#, but I cannot reproduce the network issue in the Development environment to prove my code works & cleans up after itself.
try
{
oracleCommand.ExecuteNonQuery();
}
catch(OracleException exception)
{
if(exception.Code == 3113)
CloseAndCleanup();
}
I have tried coding a PL/SQL trigger on a table that throws an ORA-3113 when I try to INSERT into the table.
CREATE OR REPLACE TRIGGER SCHEMA.TABLE
BEFORE DELETE OR INSERT OR UPDATE
ON SCHEMA.TABLE
FOR EACH ROW
DECLARE
CONNECTION_LOST_CONTACT EXCEPTION;
PRAGMA EXCEPTION_INIT (CONNECTION_LOST_CONTACT, -3113);
BEGIN
RAISE CONNECTION_LOST_CONTACT;
END;
This throws the right error, but doesn't corrupt the OracleConnection object in C#. I can still send commands to the OracleConnection and it works.
How can I accurately simulate the ORA-3113 error?
ORA-3113 means that a server process/thread that was assigned to a client unexpectedly died or was killed deliberately.
You can produce ORA-3113 error by manually killing a server process/thread. Killing session wont produce that error.
To reproduce that error you can take following steps:
1) Determine server process/thread associated with your session
select p.spid -- process ID
, s.program -- your oracle client
from v$process p
join v$session s
on p.addr = s.paddr
On the server side
2) Use orakill (windows) or kill -9 .. (Linux) to kill server thread/process
Windows example
c:\> orakill ORACLE_SID spid
After that you will get the ORA-3113 on the client side.
You can throw that exception manually from some PL-SQL code (like in a trigger).
You can kill your session.
alter system kill 'sid,serial#' immediate
You can query V$SESSION, or maybe V$MYSTAT and work it out but it's probably better to use sys_context and the USERENV namespace.
select sys_context('USERENV','SESSIONID') from dual
will get you the current session id (sid) and you can then query V$SESSION to get the serial#.
This procedure will kill the session that ran it:
create or replace procedure kill_my_session is
l_sid v$session.sid%type;
l_serial v$session.serial#%type;
begin
select sid, serial#
into l_sid, l_serial
from v$session
where sid = (select sys_context('USERENV','SESSIONID') from dual)
;
execute immediate 'alter system kill session '''|| l_sid
|| ',' || l_serial# || ''' immediate';
end;
Of course, you could always just do it manually, which might be easier.
I know the initial question was targeting system setup with Oracle on Linux. For others out there running the Oracle database XE on Windows there is an easy way to reproduce this.
Go into the running Services (found in Control Panel\System and Security\Administrative Tools) and simply stop the process named OracleServiceXE. This will also cause ORA-03113 to appear.

SQL Server: Could not find prepared statement with handle x

Recently our QA team reported a very interesting bug in one of our applications. Our application is a C# .Net 3.5 SP1 based application interacting with a SQL Server 2005 Express Edition database.
By design the application is developed to detect database offline scenarios and if so to wait until the database is online (by retrying to connect in a timely manner) and once online, reconnect and resume functionality.
What our QA team did was, while the application is retrieving a bulk of data from the database, stop the database server, wait for a while and restart the database. Once the database restarts the application reconnects to the database without any issues but it started to continuously report the exception "Could not find prepared statement with handle x" (x is some number).
Our application is using prepared statements and it is already designed to call the Prepare() method again on all the SqlCommand objects when the application reconnects to the database. For example,
At application startup,
SqlCommand _commandA = connection.CreateCommand();
_commandA.CommandText = #"SELECT COMPANYNAME FROM TBCOMPANY WHERE ID = #ID";
_commandA.CommandType = CommandType.Text;
SqlParameter _paramA = _commandA.CreateParameter();
_paramA.ParameterName = "#ID";
_paramA.SqlDbType = SqlDbType.Int;
_paramA.Direction = ParameterDirection.Input;
_paramA.Size = 0;
_commandA.Parameters.Add(_paramA);
_commandA.Prepare();
After that we use ExceuteReader() on this _commandA with different #ID parameter values in each cycle of the application.
Once the application detects the database going offline and coming back online, upon reconnect to the database the application only executes,
_commandA.Prepare();
Two more strange things we noticed.
1. The above situation on happens with CommandType.Text type commands in the code. Our application also uses the same exact logic to invoke stored procedures but we never get this issue with stored procedures.
2. Up to now we were unable to reproduce this issue no matter how many different ways we try it in the Debug mode in Visual Studio.
Thanks in advance..
I think with almost 3 days of asking the question and close to 20 views of the question and 1 answer, I have to conclude that this is not a scenario that we can handle in the way we have tried with SQL server.
The best way to mitigate this issue in your application is to re-create the SqlCommand object instance again once the application detects that the database is online.
We did the change in our application and our QA team is happy about this modification since it provided the best (or maybe the only) fix for the issue they reported.
A final thanks to everyone who viewed and answered the question.
The server caches the query plan when you call 'command.Prepare'. The error indicates that it cannot find this cached query plan when you invoke 'Prepare' again. Try creating a new 'SqlCommand' instance and invoking the query on it. I've experienced this exception before and it fixes itself when the server refreshes the cache. I doubt there is anything that can be done programmatically on the client side, to fix this.
This is not necessarily related exactly to your problem but I'm posting this as I have spent a couple of days trying to fix the same error message in my application. We have a Java application using a C3P0 connection pool, JTDS driver, connecting to a SQL Server database.
We had disabled statement caching in our the C3P0 connection pool, but had not done this on the driver level. Adding maxStatements=0 to our connection URL stopped the driver caching statements, and fixed the error.

Stored Procedure doesn't exist, or does it?

I'm having a problem:
I have a db connection where I run stored procedures on. This same connection is used to create said stored procedures earlier on.
When I attempt to call a given stored procedure, later on, I get the following message:
Could not find stored procedure
'dbo.yaf_prov_upgrade'.
The problem is it actually does exist on the database. And there's also the fact that it shows up on the SQL Server Profiler.
RPC:Completed exec
[dbo].[yaf_prov_upgrade]
#PreviousVersion=46,#NewVersion=46 .Net
SqlClient Data
Provider Nico Matrix\Nico
I was wondering what could be the causes a particular query would throw such an exception even when it exists, it's called, and the call reaches the database.
It can't be a problem with the connection because it already executed other stored procedures. It can't be a problem with the procedure because it does exist, in fact the very same application, the very same web page, created it and put it there.
Update: forgot to mention I'm used integrated security, and I did run the SP on the database with the same user the application connects with, and I had no problem running it.
So what can it be?
Your RPC completed only means that the batch submitted to SQL Server was correct and completed. It doesn't mean the stored procedure ran and executed OK.
It will be (don't argue, check) one of:
wrong permissions
wrong database context
wrong server
stored proc is in a different database
To ensure that things are the same
SELECT
##SERVERNAME,
SUSER_SNAME(),
DB_NAME(),
USER_NAME(),
OBJECT_ID('dbo.yaf_prov_upgrade')
The OBJECT_ID will be NULL if the stored proc doesn't exist in that database or you don't have permissions.
I suspect it might be a permissions issue, check up if the user name your program is executing under has execute rights to the stored proc.
I'm no expert by far on ms-sql, but I do know it keeps SPs in a global cache. Is it possible the local connection only gets the global list of SPs upon connection? Maybe reinit the connection or re-select the cache?

Categories