Example #1
0
        public void Attempt_To_Connect_Past_Session_Limit()
        {
            for (int loop = 0; loop < 1000; loop++)
            {
                var connection1 = new NpSqlConnection("Host=localhost;Port=15151");

                connection1.Open();

                connection1.Dispose();

                var connections = new ConcurrentStack <NpSqlConnection>();
                var failures    = 0;
                for (int i = 0; i < 1; i++)
                {
                    Task.Run(() =>
                    {
                        try
                        {
                            var connection = new NpSqlConnection("Host=localhost;Port=15151");

                            connection.Open();
                            connections.Push(connection);
                        }
                        catch
                        {
                            failures++;
                        }
                    });
                }

                for (int i = 0; i < 3; i++)
                {
                    NpSqlConnection connection;

                    if (connections.TryPop(out connection))
                    {
                        connection.Dispose();
                    }
                }

                failures = 0;

                for (int i = 0; i < 2; i++)
                {
                    try
                    {
                        var connection = new NpSqlConnection("Host=localhost;Port=15151");

                        connection.Open();
                        connections.Push(connection);
                    }
                    catch
                    {
                        failures++;
                    }
                }


                foreach (var connection in connections)
                {
                    connection.Dispose();
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            //Host=localhost;Port=15151
            var             commandSplitChar = new char [] { ' ' };
            var             exit             = false;
            var             hasConnection    = false;
            var             connectionString = string.Empty;
            var             defaultPort      = 15151;
            NpSqlConnection connection       = null;

            var prompt  = "npsql > ";
            var qprompt = "      > ";

            while (!exit)
            {
                Console.Write(prompt);

                var sql          = Console.ReadLine();
                var commandParts = sql.Split(commandSplitChar);

                switch (commandParts[0].ToLower())
                {
                case "quit":
                    exit = true;
                    break;

                case "connect":
                    if (commandParts.Length == 3)
                    {
                        connectionString = $"Host={commandParts[1]};Port={commandParts[2]}";
                    }
                    else if (commandParts.Length == 2)
                    {
                        connectionString = $"Host={commandParts[1]};Port={defaultPort}";
                    }
                    else
                    {
                        Console.WriteLine("Wasn't expecting that.");
                    }

                    connection = new NpSqlConnection(connectionString);
                    try
                    {
                        connection.Open();
                        hasConnection = true;
                    }
                    catch (NpSqlException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    break;

                case "disconnect":
                    if (connection != null)
                    {
                        connection.Dispose();
                        connection = null;
                    }
                    break;

                case "query":
                    if (!hasConnection)
                    {
                        Console.WriteLine("You need to connect first");
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        Console.Write(qprompt);
                        var entry = Console.ReadLine();

                        while (entry != "!s")
                        {
                            sb.Append(entry);

                            Console.Write(qprompt);
                            entry = Console.ReadLine();
                        }

                        IssueQuery(sb.ToString(), connection);
                    }
                    break;

                default:
                    if (!hasConnection)
                    {
                        Console.WriteLine("You need to connect first");
                    }
                    else
                    {
                        IssueQuery(sql, connection);
                    }
                    // Assume its sql and see what a happens
                    break;
                }
            }


            if (connection != null)
            {
                connection.Dispose();
                connection = null;
            }
        }