This repository was archived by the owner on Oct 30, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
132 lines (123 loc) · 8.31 KB
/
Copy pathProgram.cs
File metadata and controls
132 lines (123 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Diagnostics;
using Npgsql;
namespace npgsqlTester
{
class Program
{
/*
static void Main(string[] args)
{
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;Pooling=false;"; //works, pg1 without failover, due to implicity TargetSessionAttributes=Any
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Any"; //works, pg1 without failover
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Primary;"; //works, straight to pg1
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Secondary;"; //works, tests and fails over to pg2
//var connectionString = "Host=PG2,PG1;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Secondary;"; //works, straight to pg2
//var connectionString = "Host=PG2;username=robtest_login;password=robtest_password;Database=robtest;";
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Primary;Pooling=true;KeepAlive=5;"; //works, straight to pg1
var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Primary;Pooling=true;KeepAlive=5;Enlist=false;Minimum Pool Size=4;"; //works, straight to pg1
do
{
try
{
using (var pgConnection = new NpgsqlConnection(connectionString))
{
pgConnection.Open();
using (var pgTransaction = pgConnection.BeginTransaction())
{
try
{
using (var command = new NpgsqlCommand("SELECT state,COUNT(1),pg_is_in_recovery(),inet_server_addr()::text FROM pg_stat_activity GROUP BY state;", pgConnection, pgTransaction))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("{0} {1} {2} {3}", reader.GetString(0), reader.GetInt64(1), reader.GetBoolean(2), reader.GetString(3));
}
}
}
}
finally
{
pgTransaction.Rollback();
}
}
}
Console.WriteLine("q to quit, anything else to run query again!");
}
catch(Exception e)
{
Console.Error.WriteLine(e);
}
} while (true || Console.ReadLine() != "q");
}
*/
static void Main(string[] args)
{
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;Pooling=false;"; //works, pg1 without failover, due to implicity TargetSessionAttributes=Any
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Any"; //works, pg1 without failover
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Primary;"; //works, straight to pg1
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Secondary;"; //works, tests and fails over to pg2
//var connectionString = "Host=PG2,PG1;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Secondary;"; //works, straight to pg2
//var connectionString = "Host=PG2;username=robtest_login;password=robtest_password;Database=robtest;";
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Primary;Pooling=true;KeepAlive=5;"; //works, straight to pg1
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Primary;Pooling=false"; //
//var connectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Primary;Pooling=false;KeepAlive=5";
var primaryConnectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Primary;HostRecheckSeconds=1;Pooling=true;KeepAlive=5;Enlist=false;Minimum Pool Size=4;Multiplexing=false;";
var secondaryConnectionString = "Host=PG1,PG2;username=robtest_login;password=robtest_password;Database=robtest;TargetSessionAttributes=Secondary;HostRecheckSeconds=1;Pooling=true;KeepAlive=5;Enlist=false;Minimum Pool Size=4;Multiplexing=false;";
//var primaryConnectionString = connectionString;
do
{
try
{
Console.WriteLine("-------------------------------------");
using (var primaryConnection = new NpgsqlConnection(primaryConnectionString))
using (var secondaryConnection = new NpgsqlConnection(secondaryConnectionString))
{
primaryConnection.Open();
secondaryConnection.Open();
using (var pgPrimaryTransaction = primaryConnection.BeginTransaction())
using (var pgSecondaryTransaction = secondaryConnection.BeginTransaction())
{
try
{
using (var primaryCommand = new NpgsqlCommand("SELECT pg_is_in_recovery(),inet_server_addr()::text;", primaryConnection, pgPrimaryTransaction))
{
using (var reader = primaryCommand.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("primary {0} {1}", reader.GetBoolean(0), reader.GetString(1));
}
}
}
using (var secondaryCommand = new NpgsqlCommand("SELECT pg_is_in_recovery(),inet_server_addr()::text;", secondaryConnection, pgSecondaryTransaction))
{
using (var reader = secondaryCommand.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("seconda {0} {1}", reader.GetBoolean(0), reader.GetString(1));
}
}
}
}
finally
{
pgPrimaryTransaction.Rollback();
pgSecondaryTransaction.Rollback();
}
}
Console.WriteLine("q to quit, anything else to run query again!");
}
}
catch (Exception e)
{
Console.Error.WriteLine(e);
//Debugger.Break();
}
} while (Console.ReadLine().ToLower() != "q");
}
}
}