I'm a .NET develepor and I'm trying to use NPGSQL with parallel methods.
Here is my example,but I got exception "Backend sent unrecognized response type: \0"
Is it possible to use Npgsql functions with Parallel.For block?
NpgsqlConnection PGconnexion = new NpgsqlConnection(...);
string SqlParallel = "";
int OID = -1;
Parallel.For(0, 100, i =>
{
SqlParallel = string.Format(@"INSERT INTO ParallelTest(Id) VALUES({0}) RETURNING OID;", i);
using (NpgsqlCommand PGcommandParallel = new NpgsqlCommand(SqlParallel, PGconnexion))
{
try { OID = (int)PGcommandParallel.ExecuteScalar(); }
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
Console.WriteLine("Insert: {0} OID: {1}", i, OID);
});-- Table: paralleltest
-- DROP TABLE paralleltest;
CREATE TABLE paralleltest
(
id integer
)
WITH (
OIDS=TRUE
);
GRANT ALL ON TABLE paralleltest TO public;
string SqlParallel = "";
int OID = -1;
Parallel.For(0, 100, i =>
{
using (NpgsqlConnection PGconnexion = new NpgsqlConnection(...))
{
SqlParallel = string.Format(@"INSERT INTO ParallelTest(Id) VALUES({0}) RETURNING OID;", i);
using (NpgsqlCommand PGcommandParallel = new NpgsqlCommand(SqlParallel, PGconnexion))
{
try { OID = (int)PGcommandParallel.ExecuteScalar(); }
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
Console.WriteLine("Insert: {0} OID: {1}", i, OID);
}
});
--
You received this message because you are subscribed to the Google Groups "Npgsql Help" group.
To unsubscribe from this group and stop receiving emails from it, send an email to npgsql-help...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/npgsql-help/55cbf325-a1cc-4e4c-90d8-7f407b817de4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
string SqlParallel = "";
int OID = -1;
Parallel.For(0, 5, i =>
{
SqlParallel = string.Format(@"INSERT INTO ParallelTest(Id) VALUES({0}) RETURNING OID;", i);
using (NpgsqlConnection PGconnexionParallel = new NpgsqlConnection(string.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};", PGHost, PGPort, PGUser, PGPassword, PGDatabase)))
{
using (NpgsqlCommand PGcommandParallel = new NpgsqlCommand(SqlParallel, PGconnexion))
{
try
{
OID = (int)PGcommandParallel.ExecuteScalar();
Console.WriteLine("Insert: {0} OID: {1}", i, OID);
}
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
}
});
Console OutputInsert: 0 OID: 16511
Insert: 0 OID: 16512
Insert: 0 OID: 16514
Insert: 0 OID: 16515
Backend sent unrecognized response type: i
Values in ParallelTest tableoid idBR,
Christian
Hi, Christian!
The problem is that you define a connection called pgconnexionparallel but later still use the pgconnexion :-)
I hope it helps.
--
You received this message because you are subscribed to the Google Groups "Npgsql Help" group.
To unsubscribe from this group and stop receiving emails from it, send an email to npgsql-help...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/npgsql-help/117aa0ea-165b-4433-80ce-bc74e6cba7c5%40googlegroups.com.
Console OutputThe connection is not open.The connection is not open. The connection is not open.
The connection is not open.
The connection is not open.
You have to call pgconnexionparallel.open inside the using pgconnexionparallel block. That will make it work without any issues.
I hope it helps.
--
You received this message because you are subscribed to the Google Groups "Npgsql Help" group.
To unsubscribe from this group and stop receiving emails from it, send an email to npgsql-help...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/npgsql-help/d43ff1f1-2c3b-4164-a217-9004e3f2a043%40googlegroups.com.
Here thread safe solution.
Parallel.For(0, 100, i =>
{
string SqlParallel = string.Format(@"INSERT INTO ParallelTest(Id) VALUES({0}) RETURNING OID;", i);
using (NpgsqlConnection PGconnexionParallel = new NpgsqlConnection(string.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};", PGHost, PGPort, PGUser, PGPassword, PGDatabase)))
{
PGconnexionParallel.Open();
using (NpgsqlCommand PGcommandParallel = new NpgsqlCommand(SqlParallel, PGconnexionParallel))
{
try
{
int OID = (int)PGcommandParallel.ExecuteScalar();
Console.WriteLine("SqlParallel: {0} OID: {1}", SqlParallel, OID);