Npgsql throws an error: wrong connection string. Can't connect to postgreSQL [F#] - postgresql

I'm using SQLProvider to connect to my (local) PostgreSQL database in F#.
I've started with this code from fsprojects:
open FSharp.Data.Sql
open Npgsql
let [<Literal>] ResolutionPath =
__SOURCE_DIRECTORY__ + #"/../../packages/Npgsql/lib/net451/"
[<Literal>]
let connectionString = "Host=localhost;Port=5432;User ID=test;Password=test;Database=testdb;"
type PostgreSQL =
SqlDataProvider<
Common.DatabaseProviderTypes.POSTGRESQL,
ConnectionString = connectionString,
ResolutionPath = ResolutionPath,
IndividualsAmount = 1000,
UseOptionTypes = true>
When I'm trying to compile it I get this error message:
(path)/Database.fs(60,9): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: Could not create the connection, most likely this means that the connectionString is wrong. See error from Npgsql to troubleshoot: The type initializer for 'Npgsql.Counters' threw an exception.
(path)/Database.fs(60,9): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: Could not create the connection, most likely this means that the connectionString is wrong. See error from Npgsql to troubleshoot: The type initializer for 'Npgsql.Counters' threw an exception.
I've checked that user id and password and everything in connection string is correct.
I actually don't have any idea how to debug this issue.
Do you know what could be wrong?
Does it work for you without any issues?
I'm using macOS 10.12.
And lastly, in case I won't be able to fix this, are there any other methods that you would recommend for connecting to postgreSQL (with type providers)?

The error message clearly says that an exception was thrown from the type initializer for Npgsql.Counters - this is Npgsql 3.2's new support for Windows performance counters.
Unfortunately it seems that there are several issues with performance counters in various scenario (see #1447 and #1435). Because of this, version 3.2.2 (to be released this week) won't make use of them by default.
Can you please confirm that you're using 3.2.1 (and not 3.2.0), because a partial fix was introduced? If you're using 3.2.1 and getting this exception, you'll have to wait a few days for 3.2.0, and can use 3.1.10 in the meantime where the exception won't be thrown.

Related

Kotlin error connecting to Postgres : org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail

I'm trying to connect my mobile app to the Postgresql server(using PgAdmin) but it's not working and give this error. I've tried using a lot of form of url in my code but it won't do.
val url = "jdbc:postgresql://localhost/postgres"
val user = ""
val password = "password"
Class.forName("org.postgresql.Driver")
val db: Connection = DriverManager.getConnection(url, user, password)
db.close();
I think my url is wrong. How do I fix that ?
PgAdmin DB :
Error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.codemobiles.project_eva/com.codemobiles.project_eva.FeedActivity}: org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception.
Caused by: org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception.
THANKS!!!
Sol : I quit using this jdbc and use the api from server instead.
I got the same error Driver version 42.2.23, when I try with ItelliJ everything's is fine!

SocketException thrown when using linq for Npgsql EntityFramework6 model

After much effort getting my MVC5 vb.net app configured with Npgsql and EntityFramework6.Npgsql I can connect to the postgres database fine using the Entity Data Model Wizard in Visual Studio 2017, creating a new connection with Data Provider of PostgreSQL Database, entering my database server details and testing the connection (succeeds). I can also double-click the EDMX file and add tables, etc as expected. This is my first project that talks to a postgreSQL database (normally use MS SQL Server). Everything seems to be happy and fine until I encounter code that is a linq statement that attempts to query the database. My Linq statement is:
Dim l_ListOfPwrCycleCrashEvents As List(Of pwr_cycle_crash_events) = (From item In m_AnalyticsEntities.pwr_cycle_crash_events Where item.serialnumber = "0123445678" Select item).ToList
I have the statement in a try/catch block, but visual studio still reports the following:
Exception thrown:'System.Net.Sockets.SocketException' in System.dll Additional
information: A non-blocking socket operation could not be completed
immediately occurred
I cannot seem to figure out why this statement fails. I am able to use similar statements against a different EntityFramework (MS SQL Server) that is also configured in the application.
The complete method that contains the linq statement:
Public ReadOnly Property iCountOfPwrCycleCrashEvents(
ByVal a_sSerialNumber As String
) As Integer Implements IAnalytics.iCountOfPwrCycleCrashEvents
Get
Try
Dim l_ListOfPwrCycleCrashEvents As List(Of pwr_cycle_crash_events) = (From item In m_AnalyticsEntities.pwr_cycle_crash_events
Where item.serialnumber = a_sSerialNumber
Select item).ToList
If (l_ListOfPwrCycleCrashEvents IsNot Nothing) Then Return l_ListOfPwrCycleCrashEvents.Count
Catch l_Exception2 As System.Net.Sockets.SocketException
Dim askjfhakjh As Integer = 7
Catch l_Exception As Exception
Dim kjhadkjh As Integer = 1
End Try
Return 0
End Get
End Property
I am using Npgsql v4.1.1 and EntityFramework6.Npgsql v6.3.0 in an application that targets .NET Framework 4.5.1.
I can't seem to figure out why the linq statement throws this exception or how to solve the problem. What suggestions do you have?
I figured out the issue. I was using 4.5.1 framework because I thought I had a dependency that would not allow me to move to 4.5.2. I went to Package Manager Console and ran the following command:
Update-Package -ProjectName myProjectName -reinstall
By doing so, I found that I had a package that was needed but could not be loaded because it required the 4.5.2 framework (or higher). I changed the framework of the app to 4.5.2 and issued the above Package Manager command and it executed without errors. I ran the app and the linq statement no longer throws an exception.

Hibernate fail to register REF_CURSOR parameter

I am new to Hibernate. Using the 5.2.10 FINAL version connecting to Oracle 11g using the Oracle10gDialect with JPA 2.1 and ojdbc8.jar
I try to access a simple stored procedure taking a String input parameter and output a Oracle SYS_REFCURSOR.
StoredProcedureQuery call = session.createStoredProcedureCall("sp_get_profile");
call.registerStoredProcedureParameter(1, String.class, ParameterMode.IN);
call.registerStoredProcedureParameter(2, Class.class, ParameterMode.REF_CURSOR);
call.execute();
An exception occur when I access the function
ERROR SqlExceptionHelper Invalid column type: 2012
DatabaseException::error=[Error registering REF_CURSOR parameter [2]]
I try to write a simple program connecting to DB with the Oracle Driver only. I will have the same error if I register Types.REF_CUSOR as the output parameter to CallableStatement.
cs.registerOutParameter(2, Types.REF_CURSOR);
And the problem can be solved by changing to OracleTypes
cs.registerOutParameter(2, OracleTypes.CURSOR);
Anyone know what is wrong? I need to fall back to use the traditional SQL programming if I cannot get the stored procedure access success. . . please help.
Finally got it work, I should check the Oracle JDBC document first before implementation.
ojdbc8 should be good for Oracle 12c + JDK8 + JPA2.1 with Oracle12cDialect.
For Oracle 11g, need to use ojdbc6.jar
I solved that issue by debugging OJDBC and Hibernate, the building from sources a patched Hibernate CORE library (5.4.15-Final).
I have patched the method ExtractedDatabaseMetaDataImpl.supportsRefCursors which returns always false.

Sql Azure Connection String error

I am trying to connect to Sql Azure use the ADO.net connection still provided by the portal. It is of the format
var connectionstring =
"Server=tcp:abcdefghij.database.windows.net,1433;Database=[myDatabase];User ID=[myName]#abcdefghij;Password=[]myPassWord;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
When I pass this connection string to the DbContext constructor it falls over. The error message is
"The type initializer for 'System.Data.Entity.ModelConfiguration.Utilities.TypeExtensions' threw an exception."
Any ideas what the problem can be?
Thanks
Martin

Bad value for type timestamp on production server

I'm working with: seam 2.2.2 + hibernate + richfaces + jboss 5.1 + postgreSQL
I have an module which needs to load some data from the database. Easy. The problem is, on development it works fine, 100%, but when I deploy on my production server and try to get the data, an error rise:
could not read column value from result set: fechahor9_504_; Bad value for type timestamp : [C#122e5cf
SQL Error: 0, SQLState: 22007
Bad value for type timestamp : [C#122e5cf
javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute query
[more errors]
Caused by: org.postgresql.util.PSQLException: Bad value for type timestamp : [C#122e5cf
at org.postgresql.jdbc2.TimestampUtils.loadCalendar(TimestampUtils.java:232)
[more errors]
Caused by: java.lang.NumberFormatException: Trailing junk on timestamp: ''
at org.postgresql.jdbc2.TimestampUtils.loadCalendar(TimestampUtils.java:226)
I can't understand why it works on my machine (development) and why not on production. Any clues? Anyone gone through the same problem? Is exactly the same compilation
Stefano Travelli was right. I was checking the jBoss on production and there was an old jdbc driver on [jboss_dir]/common/lib from an old jwebstart application (not developed by me). Deleted that jdbc and it works fine. I should check if the old application is still needed and if so, check if it still works without the jdbc being there or with an upgraded version.
Not sure what the driver story is ..
but the problem for me show up when JDBC tries to parse bigint from the DB to
myOjbect.setDate(Date date){...}
the other "JDBC friendly" is ignorred for some reason .
myOjbect.setDate(long date){...}
So .. removing the Date setter and leaving a long one resolves the problem.
This is a Big workaround .. but may help someone out there :)