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

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.

Related

ASP .Net Connection Pooling With Oracle

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.

Extremely slow mysql connection establishment only when called from code

I am currently trying to do something that should be simple and straight-forward - connect to a database server, run a query, see if I get anything back and if so send it back to the user. This is the code I'm using to do it:
MySqlDataReader reader = MySqlHeaper.ExecuteReader(connectionString, $"SELECT * FROM table WHERE insertDateTime > '{DateTime.Now.AddSeconds(-1800).ToString("yyyy-MM-ddTHH:mm:ss")}'";
I have also tried this with a MySqlCommand and MySqlConnection object pair, and either way the result is the same - it takes approximately 7100ms to connect to the MySql server. I know that sounds like a problem that should be on ServerFault, but my testing tells me otherwise. When I use the command line MySql client to connect to my database server using exactly the same credentials and run exactly the same query I get my connection established and my data back in nothing flat. I don't know at this stage if it's a server setting or not, but here's what I've tried so far:
Rebooting the server
Restarting the MySQL server
Setting the skip_name_resolve setting to 1 in order to prevent reverse name lookups on connect
Using alternative means of querying the server (mysql command line client and MySQL Workbench)
Opening all AWS IAM permissions on the RDS instance to allow everything from the server
Nothing seems to be making any difference, so I'm at a loss to explain this terrible performance. It's also only happening when I open the connection. Running queries, inserts, what have you is lightning fast. Any suggestions anyone might have would be most helpful.
I would not expect IAM permissions to have any impact on performance. I would expect them to be either successful or not successful.
I would execute some diagnostic protocols to get more information.
1) Try a subsequent query, to see if it is an issue with the stack being initialized. Are subsequent queries faster?
2) Try a query that is just an identity query. Something that doesn't require any sort of IO.
3) Try a query from a different platform (maybe a scripting language like ruby or php)
Once you answer those it should help you narrow it down.
This is most likely caused by Connector/NET executing a slow WMI query to query connection attributes when opening the connection; this is logged as MySQL bug 80030.
As far as I know, this isn't fixed in newer versions of the driver, but you can work around it by switching to MySqlConnector, an OSS MySQL ADO.NET library.

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).

Access denied for user 'root'#'localhost' (using password: YES) in Visual Studio

Kindly bear with me. I am a Microsoft SQL Server person with loads of Visual Studio experience, but I need to get something done using a MySQL database.
I am trying to create a little tool here that will allow our developers to quickly update database records, and I am using Visual Studio to create a small Windows Form to do this.
In a Microsoft SQL Server connection string, I could write something like this:
Server=myServerAddress;Database=myDataBase;User Id=username;Password=password;
In a MySQL connection string, there appear to be multiple other options, but the first one looks basically the same:
Server=myServerAddress;Database=myDataBase;Uid=username;Pwd=password;
When I attempt to open the MySQL connection from my PC, I get the exception listed in the title (actually, it shows the Uid value and the IP Address of my PC instead of localhost, but I am hoping more people will recognize the error easier this way):
public static void MySQLi_Connect() {
m_err = null;
var str = Properties.Settings.Default.ConnStr;
try {
m_conn = new MySqlConnection(Properties.Settings.Default.ConnStr);
m_conn.Open();
} catch (MySqlException err) {
ErrorLog("MySQLi_Connect", err);
}
}
I did a search, and it seems that the Uid on MySQL needs to be granted access from the specific IP Address that the connection is being made from.
Further, I found this on the mysql.com doc pages:
If you do not know the IP address or host name of the machine from which you are connecting, you should put a row with '%' as the Host column value in the user table. After trying to connect from the client machine, use a SELECT USER() query to see how you really did connect. Then change the '%' in the user table row to the actual host name that shows up in the log. Otherwise, your system is left insecure because it permits connections from any host for the given user name.
A few things:
It looks like I can connect to MySQL by using a % setting in the Uid jp2code, but MySQL says I need to change that back right away to remove system vulnerability.
Microsoft SQL Server did not seem to require this - or, if it did, I simply never was slapped in the face with this vulnerability issue like MySQL is doing.
Now, I ask:
If this is going to be a tool used by different developers on different PCs, is it common practice to turn the blind eye to this horrendous system vulnerability?
Is this not really as big of a concern as MySQL is making it appear?
What is the best way to continue with a Windows Forms application that needs to connect from various locations? Obviously, I do not want to continuously be adding more entries for a particular application every time another developer wants to use the tool or someone tries to run it from a different PC.
You can configure the security of your MySQL server as strong as you like, usually you dont connect users but applications. So if you have your root user without password in production environment is your fault. Usually developers have access to development environment, so this is not a big deal.
Of course try to have as many users as roles you need, for your example I think one user is enough. In production use a secure config file for save a secure password and set you mysqlserver restricted.
I was having the same issue and I found out that the password wasn't correct.
GO to your sql command line and type the code below:
mydb in the line below is the name of the database you are working on.
passwd in the line has to match the password you have in c# code so in your case "password"
grant all privileges on mydb.* to myuser#localhost identified by 'passwd';
Like OP says you can wildcard the hostname portion. I used this on our dev-server (not recommended for production servers):
update mysql.user set host = '%' where host='localhost';
Then I had to restart the server to make MySQL use it (propably I could just have restarted the MySQL service).

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.

Categories