only open connection if it's closed.

This commit is contained in:
DESKTOP-GENO133\IvanPlex
2024-02-10 11:19:27 -07:00
parent 46a227453e
commit d3f94337ae
17 changed files with 72 additions and 18 deletions

View File

@@ -76,7 +76,10 @@ namespace CarCareTracker.Controllers
try try
{ {
var pgDataSource = new NpgsqlConnection(_configHelper.GetServerPostgresConnection()); var pgDataSource = new NpgsqlConnection(_configHelper.GetServerPostgresConnection());
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
InitializeTables(pgDataSource); InitializeTables(pgDataSource);
//pull records //pull records
var vehicles = new List<Vehicle>(); var vehicles = new List<Vehicle>();
@@ -421,7 +424,10 @@ namespace CarCareTracker.Controllers
try try
{ {
var pgDataSource = new NpgsqlConnection(_configHelper.GetServerPostgresConnection()); var pgDataSource = new NpgsqlConnection(_configHelper.GetServerPostgresConnection());
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
InitializeTables(pgDataSource); InitializeTables(pgDataSource);
//pull records //pull records
var vehicles = new List<Vehicle>(); var vehicles = new List<Vehicle>();

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -15,7 +15,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, body TEXT not null, emailaddress TEXT not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, body TEXT not null, emailaddress TEXT not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (userId INT, vehicleId INT, PRIMARY KEY(userId, vehicleId))"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (userId INT, vehicleId INT, PRIMARY KEY(userId, vehicleId))";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT primary key, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT primary key, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -15,7 +15,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, username TEXT not null, emailaddress TEXT not null, password TEXT not null, isadmin BOOLEAN)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, username TEXT not null, emailaddress TEXT not null, password TEXT not null, isadmin BOOLEAN)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))

View File

@@ -16,7 +16,10 @@ namespace CarCareTracker.External.Implementations
_logger = logger; _logger = logger;
try try
{ {
pgDataSource.Open(); if (pgDataSource.State == System.Data.ConnectionState.Closed)
{
pgDataSource.Open();
}
//create table if not exist. //create table if not exist.
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, data jsonb not null)"; string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, data jsonb not null)";
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource)) using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))