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

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!

Related

Flask SQLAlchemy: application crashed on postgres server reboot

I have a standard setup of flask_sqlalchemy and an AWS RDS with postgresql.
I initialize the DB adapter the standard way:
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
app = Flask(__name__)
db = SQLAlchemy()
db.init_app(app)
class Example(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(256))
If I reboot the RDS server and a SELECT starts during reboot I get:
psycopg2.OperationalError: terminating connection due to administrator
command SSL connection has been closed unexpectedly
The above exception was the direct cause of the following exception:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError)
terminating connection due to administrator command SSL connection has
been closed unexpectedly
That I understand. The DB is unavailable.
BUT: After that the application cannot recover from the situation because:
sqlalchemy.exc.InvalidRequestError: Can't reconnect until invalid
transaction is rolled back
The above exception was the direct cause of the following exception: sqlalchemy.exc.StatementError:
(sqlalchemy.exc.InvalidRequestError) Can't reconnect until invalid
transaction is rolled back
I understand the application throws exceptions during reboot.
But what puzzles me that it never recovers.
There is no write transaction to the DB! I don't understand what rollback the exception is describing.
Edit:
This is how all commits are implemented:
try:
db.session.commit()
except Exception as e:
db.session.rollback()
But there is no commit triggering the exception. Only SELECTS.
Do this and it should fix it for you session.rollback().
If you want to find this error, wrap it into a try/except-block and perform a session.rollback().
EDIT:
So I test some of the stuff and I believe the reason for the error lies within the try/except block. Ideally the doc says this is how you should be implementing inserts:
Create the Python object
Add it to the session
Commit the session
e.g.
>>> db.session.add(User(name="Flask", email="example#example.com"))
>>> db.session.commit()
Likewise for deletes:
>>> db.session.delete(me)
>>> db.session.commit()
For querying you do not need to commit exclusively, the query attribute in Flask-SQLAlchemy does it for you on the Model class.
so for selecting you just do:
users = User.query.all()
Which in your case is:
something = Example.query.all()
One more thing, can you please clarify or give more info on why you are implementing commits like this:
try:
db.session.commit()
except Exception as e:
db.session.rollback()
PS: I might have misread your last edit, my bad!

Postgres giving error on update

I am using Postgresql persistence provider with Thingworx. When I am trying to run a service for ConnectivityActivation I am getting the following error
Cause: Execute Update failed: org.postgresql.util.PSQLException: ERROR: syntax error at or near "on"
Position: 145
When I tried this on some other thingworx server then I wasn't getting this error.
I am thinking in the direction that this might be some issue with the permissions but I am not able to find the root cause for this issue.
Please let me know if my understanding is correct or there could be some issue with the postgresql database.
The query run is
insert into transactiondata
("transId","category","api","devId","timestamp","state","country")
values ('123','category','api','123456','','Active','India')
on CONFLICT ("transId")
DO UPDATE SET "state" = 'Disabled', "timestamp" = '' ,"category" = 'NA';

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

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.

Flyway cannot connect to db after Heroku Postgres upgrade

I am upgrading my heroku database from a hobby dev to Standard 0 (using the official instructions https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#upgrade-with-pg-copy-default).
All went well, until I promoted the new database and restarted the app. I then get the following error:
o.s.boot.SpringApplication : Application startup failed
...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
...
Caused by: org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
...
Caused by: org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "54.xxx.xx.xxx", user "u94bf9vxxxxxx", database "d2mqk0b6xxxxxx", SSL off
...
If I swap back to the old database again, everything works again. The only thing that I am changing is the promoted database.
Is there a difference between connecting to hobby and standard databases that I need to be aware of?
The relevant part of my application.yml looks as follows:
spring:
datasource:
driverClassName: org.postgresql.Driver
url: ${JDBC_DATABASE_URL}
username: ${JDBC_DATABASE_USERNAME}
password: ${JDBC_DATABASE_PASSWORD}
flyway:
enabled: true
locations: classpath:db/migrations
Any suggestions on how I can debug this would be very welcome too.
Looks like you aren't connecting with SSL where it is required by Heroku PostgreSQL installs.
See Herokus documentation on SSL for PostgreSQL.
See also Herokus documentation for enabling SSL on JDBC connections.
You will need to add something like &ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory to your JDBC URL.

MongoInternalException: DBPort.findOne failed while running on GAE localserver

I am trying to connect to remote MongoDB (mongolab) from my local GAE server (localhost/8888). I am using morphia and my mongodb driver version is 2.4. My code looks like this:
Mongo m = new Mongo("xyz.mongolab.com",);
Datastore datastore = new Morphia().createDatastore(m, "staging","uname","password".toCharArray());
This throws the following exception :
com.mongodb.MongoInternalException: DBPort.findOne failed
at com.mongodb.DBPort.findOne(DBPort.java:153)
at com.mongodb.DBPort.runCommand(DBPort.java:159)
at com.mongodb.DBTCPConnector.testMaster(DBTCPConnector.java:371)
at com.mongodb.Mongo.(Mongo.java:167)
Caused by: java.io.IOException: couldn't connect to [xyz.mongolab.com/:] bc:java.net.SocketException: Operation failure: setSocketOptions: Not yet implemented
at com.mongodb.DBPort._open(DBPort.java:205)
Does somebody know why this is happening ?
it was a problem with using the old mongodb driver.. works after i upgraded..