Why Npgsql sometimes not responding? - npgsql

I using Npgsql 3.2.2
I connect to database of Web server:
<add key="CONNECTION_STRING_WEBSERVER" value="Server=abctest.com;Port=5432;UserId=postgres;Password=postgres;Database=testdatabase;CommandTimeout=300;" />
My query to get data:
Dim sql="Select * from table1;"
my function:
private DataTable getData(string tableName)
{
DataSet ds = null;
DataTable tbl = null;
try
{
if (m_ConnectionExecute == null)
{
m_ConnectionExecute = new NpgsqlConnection(m_connString_web);
}
if (m_ConnectionExecute.State != ConnectionState.Open)
{
m_ConnectionExecute.Open();
}
NpgsqlDataAdapter adapter = new NpgsqlDataAdapter();
NpgsqlCommand command = null;
try
{
command = CreateCommand(m_Parameters);
command.CommandText = m_commText;
command.Connection = m_ConnectionExecute;
adapter.SelectCommand = command;
ds = new DataSet();
adapter.Fill(ds, "Table1");
tbl = ds.Tables[0];
tbl.TableName = tableName;
ds.Tables.Clear();
}
catch (SqlException ex)
{
ds = null;
}
finally
{
if ( m_ConnectionExecute != null && m_ConnectionExecute.State != ConnectionState.Closed)
{
m_ConnectionExecute.Close();
}
}
}
catch (Exception e)
{
ds = null;
tbl = null;
}
return tbl;
}
I using Timer : 5s will call function getData.
But sometimes, function getData not respond and my program can't continue next process.
The problem only occur when Timer run some days and database put on a web server.
Notes: I have 5 Timer run auto access database.
what is the cause? or limit of postgresql?
Why Npgsql sometimes not responding?

Related

Not able to insert data into table using SqlDataAdapter and stored procedure

I have code to pick data from a Windows form application and load into my SQL Server table. I wrote the query shown here, but the insert is not working. On debugging I am getting a null exception error. I am not able to figure out what is causing the null exception. Please help
public bool insert(contactClass c)
{
bool isSuccess = false;
dt = new DataTable();
try
{
// while debugging control goes to catch due to null exception
SqlDataAdapter adp1 = new SqlDataAdapter("sp_contact", db.con);
adp1.InsertCommand.CommandType = CommandType.StoredProcedure;
adp1.InsertCommand.Parameters.AddWithValue("#flag", 2);
adp1.InsertCommand.Parameters.AddWithValue("#firstname", c.FirstName);
adp1.InsertCommand.Parameters.AddWithValue("#lastname", c.LastName);
adp1.InsertCommand.Parameters.AddWithValue("#contactno", c.ContactNo);
adp1.InsertCommand.Parameters.AddWithValue("#address", c.Address);
adp1.InsertCommand.Parameters.AddWithValue("#gender", c.Gender);
adp1.Update(dt);
if (dt.Rows.Count > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch(Exception e)
{
string msg = e.Message;
Console.WriteLine(msg);
}
}

Execute Reader is not returning a result

Can you please help me see what is wrong with my code? If I run the stored procedure using the a parameter for Id, I get a result in SQL Server. But when I use the code below using the same value for Id, in my if(rdr.HasRows).. I get a false.
public Student Find(int key)
{
string connectionPath = ConnectionStrings.DbConnection;
Student student = null;
try
{
using(var sqlCon = new SqlConnection(connectionPath))
{
sqlCon.Open();
using(var cmd = new SqlCommand("Sp_FindStudent", sqlCon))
{
cmd.Parameters.AddWithValue("#Id", key);
using(var rdr = cmd.ExecuteReader(CommandBehavior.SingleResult))
{
if (rdr.HasRows)
{
while (rdr.Read())
{
student = new Student
{
Age = Convert.ToInt32(rdr["Age"]),
FirstName = rdr["FirstName"].ToString(),
LastName = rdr["LastName"].ToString(),
Gender = rdr["Gender"].ToString()
};
}
}
}
}
}
}
catch(Exception ex)
{
throw ex;
}
return student;
}
If I try to get all records, I don't get any problems:
public IEnumerable<Student> GetAll()
{
var studentList = new List<Student>();
try
{
string connectionPath = ConnectionStrings.DbConnection;
using(var sqlCon = new SqlConnection(connectionPath))
{
using(var cmd = new SqlCommand("Sp_GetStudents", sqlCon) { CommandType = CommandType.StoredProcedure})
{
sqlCon.Open();
using(var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (rdr.Read())
{
studentList.Add
(
new Student
{
Id = Convert.ToInt32(rdr["Id"]),
Age = Convert.ToInt32(rdr["Age"]),
FirstName = rdr["FirstName"].ToString(),
LastName = rdr["LastName"].ToString()
}
);
}
}
}
}
}
catch(Exception ex)
{
throw ex;
}
return studentList;
}
This is using asp.net core
Your code for Find lacks the definition that this is a stored procedure that you're calling.
If you look at your GetAll, you have:
using(var cmd = new SqlCommand("Sp_GetStudents", sqlCon) { CommandType = CommandType.StoredProcedure})
defining this SqlCommand to be a stored procedure - this setting is missing from your Find code:
using(var cmd = new SqlCommand("Sp_FindStudent", sqlCon))
I'm pretty sure it'll work if you add that:
using(var cmd = new SqlCommand("Sp_FindStudent", sqlCon) { CommandType = CommandType.StoredProcedure}))

Having trouble with SQL Delete on SQL Server using C#

I am running this code but it throws an exception and I am not sure why. Any help would be appreciated. I checked the ID and it was the right ID for the record.
protected void DeleteSQLDB(int id)
{
String ConnString = GetConnectAccess();
try
{
using (SqlConnection m_dbConnection = new SqlConnection(ConnString))
{
String sql = "DELETE FROM tblStudent WHERE ID=" + id;
using (SqlCommand cmd = new SqlCommand(sql, m_dbConnection))
{
m_dbConnection.Open();
cmd.ExecuteNonQuery();
m_dbConnection.Close();
}
}
}
catch (Exception ex)
{
Response.Redirect("somwhere");
}
finally
{
}
}
I solved the problem. The reason was that the record was referenced in other tables so I had to remove the references before I could remove the record. Thanks user7396598 for advice about running query manually. This is the code which removes the conversations first and then the student record:
//This deletes the archived student, First any conversations need to be deleted before the record can be removed.
protected void DeleteSQLDB(object id,String studentID)
{
// Response.Redirect(studentID);
String ConnString = GetConnectAccess();
try
{
using (SqlConnection m_dbConnection = new SqlConnection(ConnString))
{
String sql = "DELETE FROM tblConversations WHERE StudentID=#studentID";
using (SqlCommand cmd = new SqlCommand(sql, m_dbConnection))
{
cmd.Parameters.AddWithValue("#studentID", studentID);
m_dbConnection.Open();
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
}
finally
{
DeleteSQLDB2(id);
}
}
protected void DeleteSQLDB2(object id)
{
// Response.Redirect(studentID);
String ConnString = GetConnectAccess();
try
{
using (SqlConnection m_dbConnection = new SqlConnection(ConnString))
{
String sql = "DELETE FROM tblStudent WHERE ID=#ID";
using (SqlCommand cmd = new SqlCommand(sql, m_dbConnection))
{
cmd.Parameters.AddWithValue("#ID", id);
m_dbConnection.Open();
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
}
finally
{
Response.Redirect("studentgrid.aspx");
}
}

Design / Sonar: "Change this condition so that it does not always evaluate to true"

I'm playing with Sonarqube plugin for Jenkins. How can I effectively solve this trivial violation he is complaining about without changing the logic?
Note: I need to validate the connections separetely ( ConnectionManager, statistics, keepAlive, .. ).
`
public void executeProcedure( final RequestProcessor consumer ) throws SQLException {
final String procedure = consumer.getProcedure();
final String idUrl = consumer.getIdUrl();
final PreparedStatementRegisterer stmRegisterer = consumer.getRegisterer();
// Autoclosable removed to allow ad hoc connection validation
Connection conn = null;
boolean execSuccess = false;
try{
conn = newConnection();
conn = checkOrChangeConnection(conn, false);
boolean hasResultSet = false;
try( CallableStatement statement = (OracleCallableStatement)conn.prepareCall(procedure) ){
...
stmRegisterer.prepareStatement(statement, idUrl);
statement.setQueryTimeout(QUERY_TIMEOUT);
hasResultSet = statement.execute();
execSuccess = true;
if(hasResultSet){
...
try ( ResultSet rs = statement.getResultSet() ) {
while ( rs.next() ) {
consumer.handleRow( rs );
}
}
}else{
...
consumer.getFieldsFromResult( statement );
}
}
}catch(Exception ex){
LOGGER.log( LogEntries.StorProcErr, ex.getMessage() );
throw new Exception( (!execSuccess ? "Error preparing and executing statement.":"Error during results reading.")+" Cause: "+ex) );
}finally{
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
System.out.println("\n Error closing connection on executeStoredProc. Cause: "+e+" \n"); // log
}
}
}
My idea is to add some more logging and re-throw the same exception after the log "StorProcErr". Is there a better approach?
Thanks

Is there a utility to dump an existing log4j log file into a relational database?

Seems, like a very basic thing, but I could not find it.
I have a bunch of log4j/log4net log files. I would like to dump them into a database in order to be able to analyze them with ease.
I thought I would find a tool to do it in no time, apparently I am wrong.
Does anyone know of such a tool?
OK, so I found no utility. Had to write my own. Of course, it is strictly tailored to my immediate needs (time is money), however, it can save you a bit of time to start your own, in case of a need. Here is the complete code in C#:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication3
{
class Program
{
public class LogEntry
{
private const string PATTERN = #"^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\.\d{4}) (\S+) \[(\d+)\] (\w+) (\S+) - (.*)$";
private static readonly Regex s_regex = new Regex(PATTERN, RegexOptions.Compiled);
public DateTime TS;
public string Machine;
public int Thread;
public string Level;
public string Logger;
public string Message;
public static LogEntry TryCreate(string line)
{
var match = s_regex.Match(line);
return match.Success ? new LogEntry
{
TS = DateTime.ParseExact(match.Groups[1].Value, "yyyy-MM-dd HH:mm:ss.ffff", CultureInfo.InvariantCulture),
Machine = match.Groups[2].Value,
Thread = int.Parse(match.Groups[3].Value),
Level = match.Groups[4].Value,
Logger = match.Groups[5].Value,
Message = match.Groups[6].Value,
} : null;
}
public void AppendToMessage(string line)
{
Message += Environment.NewLine + line;
}
}
static void Main()
{
const string SQL = #"
INSERT INTO log ( ts, machine, thread, level, logger, message, journalId)
VALUES (#ts, #machine, #thread, #level, #logger, #message, #journalId)
";
using (var connection = new SqlConnection("server=localhost;database=misc;uid=SantaClaus;pwd=MerryChristmas"))
{
connection.Open();
using (var command = new SqlCommand(SQL, connection))
{
var tsParam = new SqlParameter("#ts", SqlDbType.DateTime);
var machineParam = new SqlParameter("#machine", SqlDbType.NVarChar, 32);
var threadParam = new SqlParameter("#thread", SqlDbType.Int);
var levelParam = new SqlParameter("#level", SqlDbType.NVarChar, 10);
var loggerParam = new SqlParameter("#logger", SqlDbType.NVarChar, 128);
var messageParam = new SqlParameter("#message", SqlDbType.NVarChar, -1);
var journalIdParam = new SqlParameter("#journalId", SqlDbType.Int);
command.Parameters.Add(tsParam);
command.Parameters.Add(machineParam);
command.Parameters.Add(threadParam);
command.Parameters.Add(levelParam);
command.Parameters.Add(loggerParam);
command.Parameters.Add(messageParam);
command.Parameters.Add(journalIdParam);
// Call Prepare after setting the Commandtext and Parameters.
command.Prepare();
int i = 0;
foreach (var file in Directory.GetFiles(#"c:\tmp\dfbje01"))
{
journalIdParam.Value = OpenJournal(connection, file);
command.Transaction = connection.BeginTransaction();
foreach (var e in GetLogEntries(file))
{
tsParam.Value = e.TS;
machineParam.Value = e.Machine;
threadParam.Value = e.Thread;
levelParam.Value = e.Level;
loggerParam.Value = e.Logger;
messageParam.Value = e.Message;
command.ExecuteNonQuery();
++i;
if (i == 1000)
{
i = 0;
command.Transaction.Commit();
command.Transaction = connection.BeginTransaction();
}
}
command.Transaction.Commit();
CloseJournal(connection, journalIdParam.Value);
}
}
}
}
private static void CloseJournal(SqlConnection connection, object id)
{
const string SQL = "UPDATE journal SET done = 1 WHERE id = #id";
using (var command = new SqlCommand(SQL, connection))
{
command.Parameters.Add(new SqlParameter("#id", id));
command.ExecuteNonQuery();
}
}
private static object OpenJournal(SqlConnection connection, string filePath)
{
const string SQL = "INSERT INTO journal (filePath) OUTPUT inserted.id VALUES (#filePath)";
using (var command = new SqlCommand(SQL, connection))
{
command.Parameters.Add(new SqlParameter("#filePath", filePath));
return command.ExecuteScalar();
}
}
private static IEnumerable<LogEntry> GetLogEntries(string filePath)
{
LogEntry prev = null;
foreach (var line in File.ReadLines(filePath))
{
var logEntry = LogEntry.TryCreate(line);
if (logEntry != null)
{
if (prev != null)
{
yield return prev;
}
prev = logEntry;
}
else if (prev != null)
{
prev.AppendToMessage(line);
}
else
{
// Oops
Console.WriteLine(line);
}
}
if (prev != null)
{
yield return prev;
}
}
}
}
Mind trying out the filtering, search, colorizing features of the latest developer snapshot of Chainsaw? It has a good number of features which may avoid the need to use a DB. If you use a VFSLogFilePatternReceiver, it can parse and tail any regular text file, including those created by log4net.
Latest developer snapshot of Chainsaw is available here:
http://people.apache.org/~sdeboy

Resources