一些棘手的快速的方法来验证Oracle数据库的连接 [英] Some tricky quick way to validate oracle db connection

查看:182
本文介绍了一些棘手的快速的方法来验证Oracle数据库的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WCF服务需要检查的连接可用了,我们可以使用它。我们有许多远程DBS。它们的连接有时怪异的,不能用于查询数据或不服别的。 因此,举例来说,这是经常使用的连接字符串:

My WCF service need to check is connection available now and can we work with it. We have many remote dbs. Their connection are weird sometimes and can't be used to query data or smth else. So, for example this is regular connection string used:

User Id=user;Password=P@ssw0rd;Data Source=NVDB1;Connection Timeout=30

下面是我们的服务的方法,用于获取

Here is service method, used for getting

    public List<string> GetAliveDBs(string city)
    {
        if (String.IsNullOrEmpty(city))
            return null;                        

        List<string> cityDbs = (from l in alldbs where !String.IsNullOrEmpty(l.Value.city) && l.Value.city.ToUpper() == city.ToUpper() select l.Value.connString).ToList();            

        // There is no such city databases
        if (cityDbs.Count == 0)
            return null;

        ReaderWriterLockSlim locker = new ReaderWriterLockSlim();

        Parallel.ForEach(cityDbs, p =>
        {
            if (!IsConnectionActive(p.connString))
            {
                locker.EnterWriteLock();
                try
                {
                    cityDbs.RemoveAt(cityDbs.IndexOf(p));
                }
                finally
                {
                    locker.ExitWriteLock();
                }
            }
        });

        return cityDbs;
    }

    static public bool IsConnectionAlive(string connectionString)
    {
        using (OracleConnection c = new OracleConnection(connectionString))
        {
            try
            {                    
                c.Open();
                if ((c.State == ConnectionState.Open) && (c.Ping()))
                    return true;
                else
                    return false;
            }
            catch (Exception exc)
            {
                return false;                 
            }
        }
    }

我用devart组件与Oracle数据库进行通信。 希望对你有所帮助,伙计们!在此先感谢!

I use devart components to communicate with Oracle DB. Hope for your help, guys! Thanks in advance!

推荐答案

如果目标是简单地确定服务器是否住在的IP地址或主机名,那么我建议你平安(NO 3握手和开销较少比UDP报文)。您可以使用 System.Net.NetworkInformation.Ping 类(看到它的这个为例文档)。

If the goal is to simply determine if a server lives at the IP Address or host name then I'd recommend Ping (no 3 way handshake and has less overhead than a UDP message). You can use the System.Net.NetworkInformation.Ping class (see its documentation for an example) for this.

如果你想证明其实是有一些监听共同甲骨文端口上,我建议使用任一的 System.Net.Sockets.TcpClient 或的 System.Net.Sockets.Socket 类(它们的文档也提供了范例)提供这一点。

If you're looking to prove that there is actually something listening on the common Oracle port, I would suggest using either the System.Net.Sockets.TcpClient or System.Net.Sockets.Socket class (their documentation also provides examples) to provide this.

要做到这一点(目前为止)最简单的方法是只打开使用了C#中的Oracle API的连接。有一个非常好的教程,包括code的此处。它涵盖的不仅仅是连接,但你应该能够去掉连接部分从休息,以满足您的需求。

The simplest way to do this (by far) is to just open a connection using the Oracle API for C#. There is a very good tutorial that includes code here. It covers more than just the connection but you should be able to strip out the connection portion from the rest to fit your needs.

这篇关于一些棘手的快速的方法来验证Oracle数据库的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆