I want to check availability of database connection so that I could put my application in specific modes; online mode and offline mode.
In order to do so, I try to open an OleDB connection (the database is Oracle) and if it is successful, the application shall run in online mode. However, if the database is down, opening the connection shall only be closed after specific period of time due to timeout.
Is there specific way of doing this without having to wait for the timeout? Or maybe, specify the timeout interval?
There is no other way then connecting to the database service.
However you could Mmdify the ConnectionTimeout property of your Connection instance before opening it to tweak the amount of time you want to wait.
On the other hand you could always start with the offline mode, do the connection check async and change the application behavior when the check was succeeded.
Related
In our enviroment we use SQLServer Always on cluster with two servers.
One of them is for write, second for reading. In application services SqlConnection opens and closes every second for short query execution. But after switching or turning off one of the servers in Always On cluster I began get an exceptions from my application services.
This tells that it can not insert any data in read only database. I suppose that main reason for this is connection pool inside SqlConnection implementation.
So the question is how to reset that connection pool manually. Or if there another kind of problem - let me know what do you think about this behavior.
You need to set MultiSubnetFailover = True in connection string and implement retry logic:
If a SqlClient application is connected to an AlwaysOn database that
fails over, the original connection is broken and the application must
open a new connection to continue work after the failover.
SqlClient Support for High Availability, Disaster Recovery
Also:
Setting MultiSubnetFailover to true isn't required with .NET Framework
4.6.1 or later versions.
I have a windows form application (developed in VS Express 2013) that connects through an instance of SQL Server 2012 to a database. Both the application and database are on my local system; I just needed a GUI to more easily interface with this very large database that stores my research data. When I initially compile and deploy the application, it works fine and has no connection problems with the database. However, if I then attach the database in SQL Server Management Studio (which I sometimes want to do) I get an error the next time I try to use the application - "Cannot open database ..... requested by the login, the login failed". I get this error even if I take the database offline and detach it before quitting SSMS. And just to be clear - I'm not making any changes to the database in SSMS, I'm just looking at the data. The connection string used by VS is Data Source (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CollectionMetricsDatabase.mdf;Integrated Security=True.
Any idea what the problem is?
Maybe the instance of SQL Server 2012 has a max number of concurrent connections and you exceed that with your two connections at once (SSMS connection and application's connection). Check the settings in SSMS.
Another thing to try - Based on your connection string, you are using Integrated Security, which in my experience means the application connects using Windows credentials. That can be fragile, because it means that whatever 'user' runs the application (if hosted in IIS, this will be the App Pool user) is the one whose Windows credentials are used. I would suggest creating a SQL login user (in SSMS) for the database, with db_owner access, and then changing the application's connection to be username/password based instead. You may need to enable Mixed Authentication for the SQL Server instance in SSMS if it doesn't already allow it.
You need to connect in SSMS with the localDB connection string. Attaching it will prevent it from working with localDB.
In SSMS, create a connection to (LocalDB)\v11.0. You should see your DB there already.
The problem is that you are connecting to the database file directly not through SQL Server.
Only one user can have a lock on the file at the time.
You need to recreate a connection string for your app.
That is why you were needing to detach the database.
Since this is only a personal project you could live with things as they are, at least you know the server is only running when you need it.
create connection msdn
We have 2 legacy systems - One in C++ and other in C# that connect to the same access database. The access database is not password protected and we use the following connection string to connect to the databases
Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=False ;Data Source= AlarmHistory.mdb
C# application polls a table in the database every 10 seconds. Once in while , the C# application crashes with the error message -
System.Data.OleDb.OleDbException: Cannot start your application. The workgroup information file is missing or opened exclusively by another user.
The log analysis revealed that both the applications are not accessing the database simultaneously ... Any idea on what could cause such a situation... Please Help
They might not be accessing the database at exactly the same time, but that error message indicates that they (or something else) is accessing the lock file at the same time. Is the poll opening and closing the connection each time? You could move the open and close to outside the timer so that the connection is opened and remains open for the life of the application.
I have a C# application that uses a localhost DB (MySQL).
Now, when I create the executable I´m assuming that the receptor computer MUST have the exact DB with the the same name and tables, also, must have running WAMP or XAMPP.
If one of this conditions is not accomplished the program will crash horribly, with the errors of Windows/C#.
I could put exceptions for every case, but I´m fearful that I would hide other errors putting exceptions for everything!
With production software, how do you manage this? With exceptions? Writing a manual for the user? etc?
During bootstrapping, I recommend check to see if a DB Connection can be created (in my case, SQL Server), given the database connection string defined in an app.config. Initially, you should do some version checking on the database. If the database can't be found, attempt to create it. if i'ts out of date, attempt to upgrade it. If this process fails, then your database engine instance isn't installed or is unresponsive. For my application case, I exit the program, as there's nothing else to do if the DB can't be accessed.
Once past this point, I generally assume that the DB connection is active.
I am writing an Application that executes sql queries on a database in real time.
If I setup mirroring of the SQL Server and setup a FailoverPartner in the connection string and if the primary database goes down, will the secondary kick in automatically and therefore I do not have to re-open a connection or clear anything down?
Also how does it handle situations where a INSERT statement is running and the database goes down? Does the secodary pick it up or is it lost forever? Would it be safer to do transactional based insert statements?
Every form of High Availability (mirroring, clustering) will have a transaction boundary: every transaction in-flight at the moment of failover will be rolled back. This is not a limitation, is a feature. It would be impossible to write correct applications if this would not be true.
When a failover occurs, every connection using the database is cut. The clients have to re-establish new connections with the new principal, read again current state from the database and start applying new operations, on the current state. Correctly written applications (ie. transactional) will not have any issue with this. Only poorly coded apps can lose data.