NpgsqlConnection : Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host - npgsql

I am getting error 'Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.' when connecting to PostgreSQL db from .net console application. As far as I did R&D and tried different things, I found that it may be because of = in password as other connection string works fine that doesn't have = in password. Is there any way to pass = in password tried sending %3D instead of = but it didn't work. Password is abc=xyz. Issue looks like with Password=abc=xyz, tried changing it to Password=abc%3Dxyz as well. pgcon.Open(); gives this error.
string pgconnectionstring="Server=someserver.azure.com;Username=dev#postgres;Database=dbname;Port=5432;Password=abc=xyz;SSLMode=Require";
using (NpgsqlConnection pgcon = new NpgsqlConnection(pgconnectionstring))
{
pgcon.Open();
}
NpgsqlConnection doesn't have property password to be set from outside, Is there any way to handle = or other special chars or set password differently?

Issue was not because of =, It was because of DB name. Seems like DB name should be case sensitive. The actual db name is Field-Service. And I was setting it like Field-service, s in small which led to the issue. Though exception message led to wrong direction as it gave error connection closed by remote which generally comes in case of wrong password instead should have given something like DB doesn't exists.

Related

How does Dapper execute query without explicitly opening connection?

We are using Dapper for some data access activity and are using the standard recommended approach for connecting to database as follows:
public static Func<DbConnection> ConnectionFactory = () => new SqlConnection(ConnectionString);
However, if we try and execute a statement, in the docs it show that you need to first state:
using (var conn = ConnectionFactory())
{
conn.Open();
var result = await conn.ExecuteAsync(sql, p, commandType: CommandType.StoredProcedure);
return result;
}
That means, you have to explicitly open the connection. However, if we leave out the statement conn.open(), it also works and we are worried if in such cases the connection may not be disposed of properly.
I would appreciate any comments as to how the SQL gets executed without explicitly opening any connection.
Dapper provide two ways to handle connection.
First is - Allow Dapper to handle it.
Here, you do not need to open the connection before sending it to Dapper. If input connection is not in Open state, Dapper will open it - Dapper will do the actions - Dapper will close the connection.
This will just close the connection. Open/Close is different than Dispose. So, if you really want to Dispose the connection better switch to second way.
Second is - Handle all yourself.
Here, you should explicitly create, open, close and dispose the connection yourself.
Please refer to following links for more details:
https://stackoverflow.com/a/51138718/5779732
https://stackoverflow.com/a/41054369/5779732
https://stackoverflow.com/a/40827671/5779732

IFSFile when user PASSWORD *NONE

I am facing some troubles while trying to create an IFSFile using IFSFile object from JT400.jar. The problem that I am facing is when the process that calls the JAVA is called by a BATCH user(without login enabled into AS400 machine).
Exception given
Password is *NONE.:XXXXXX com.ibm.as400.access.AS400SecurityException: Password is *NONE.:XXXXXX
at com.ibm.as400.access.AS400ImplRemote.returnSecurityException(AS400ImplRemote.java:2219)
at com.ibm.as400.access.CurrentUser.getUserInfo(CurrentUser.java:79)
at com.ibm.as400.access.AS400ImplRemote.getPassword(AS400ImplRemote.java:1411)
at com.ibm.as400.access.AS400ImplRemote.signon(AS400ImplRemote.java:2507)
at com.ibm.as400.access.AS400.sendSignonRequest(AS400.java:3351)
at com.ibm.as400.access.AS400.promptSignon(AS400.java:2938)
at com.ibm.as400.access.AS400.signon(AS400.java:4246)
at com.ibm.as400.access.AS400.connectService(AS400.java:1336)
at com.ibm.as400.access.IFSFile.chooseImpl(IFSFile.java:630)
at com.ibm.as400.access.IFSFile.copyTo(IFSFile.java:729)
at com.ibm.as400.access.IFSFile.copyTo(IFSFile.java:699)
Code used:
AS400 as400 = new AS400("localhost");
//Obtain the template path
String templatePath ="/HOME/XXX/auth.txt";
IFSFile templateAuth = new IFSFile(as400,templatePath);
templateAuth.copyTo(fileXML + ".xml");
I have check some opened threads but no results obtained. (find below the threads commented)
JT400 Read File From IFS with user without password
Java IFSFile on Iseries testing over PC
There is any option to generate an IFSFile when the process is called by a BATCH user(note that when the process is called by a user with login enabled, the process is working as expected)
I need something similar to what is done when a JDBCAppender is created, JDBCAppender object allows setUser(null) and setPassword(null) to enable batch users to write into a table.
Thanks all!
I have recently had a similar problem when trying to use CommandCall whilst running under a profile with PASSWORD is *NONE. I (eventually) solved it by using the native Java toolbox at /QIBM/ProdData/Java400/jt400ntv.jar (which happens to be a symbolic link). I didn't have to make any code changes:
AS400 as400 = new AS400();
CommandCall cc = new CommandCall(as400);
String wrkenvvar_cmd = MessageFormat.format("ADDENVVAR ENVVAR('JAVA_HOME') VALUE(''{0}'') REPLACE(*YES)",path);
boolean ok = cc.run(wrkenvvar_cmd);
I hope that helps.

Script Component - ODBC exception password incorrect - package validation

I'm trying to insert some rows into Filemaker using Script Component. I followed this article Creating an ODBC Destination with the Script Component
When I edit the script here I set the connection string:
public override void AcquireConnections(object Transaction)
{
string connectionString;
//connectionString = this.Connections.FmConnection.ConnectionString;
odbcConn = new OdbcConnection("uid=someUID;Dsn=FM;pwd=somepassword");
odbcConn.Open();
}
I get the connection string from the this.Connections object and set it to the new OdbcConnection object. This does not work as I keep getting the exception. I tried setting the connection string manually as you can see above. I still get the exception.
In other parts of my package I use the connection manager to Filemaker and it works. Just not in this script component.
The exceptions I'm getting is:
Error at MyPackageName: [Filemaker] Password Incorrect
What is wrong?

QSqlDatabase Connecting to Multiple Databases

I am having issues attempting to connect to two different databases in one Qt Application. I have my information database that stores all the information collected by the application and the new Log database which allows me to track all the changes that occur to the Application, button presses, screen loads etc, for easy debugging after its release. Separately, the databases work perfectly, but when I try to use both of them, only one will work. I read that this could be because I wasn't naming the connections and obviously only the most recently connected database could use the default connection. However when I give the databases names they wont work at all, isOpen() will return true on both, but as soon as they attempt to execute a query I get the errors
"QSqlQuery::prepare: database not open"
"QSqlError(-1, "Driver not loaded", "Driver not loaded")"
My two database declarations are:
database_location = filepath.append("/logger.sqlite");
logDB = QSqlDatabase::addDatabase("QSQLITE", "LoggerDatabaseConnection");
logDB.setHostName("localhost");
logDB.setDatabaseName(database_location);
for the Logger Database connection and :
database_location = filepath.append("/db.sqlite");
db = QSqlDatabase::addDatabase("QSQLITE", "NormalDB");
db.setHostName("localhost");
db.setDatabaseName(database_location);
Also when I am running the first query on the databases to see if their tables exist I am using
QSqlQuery query("LoggerDatabaseConnection");
and likewise for the normal database, but I am still getting connection issues even after declaring the database connection to run the query on.
The database used for the application is declared as a static QSqlDatabase in a namespace to create a global effect, so everyone can access it, that was a previous programmer, and I created the Log database as Singleton with a private database connection. Like I said both versions of the code work separately but when they are together they are fighting each other. I know there is a huge debate over the proper design of Singleton vs Dependecy Injection, but again the code works separately so I am happy with how it is designed for now. If there is any missing information or if you have any ideas, please let me know. Thank you.
QSqlQuery query("LoggerDatabaseConnection");
The first parameter of the constructor is the query, not the connection name. It will use the default connection since you specified no database object.
Try something like this:
QSqlQuery query1("YourFirstQuery", db);
QSqlQuery query2("YourSecondQuery", logDB);
Important: Also do not forget to open and close the database before / after using it by calls to QSqlDatabase::open() and QSqlDatabase::close().
The correct way to have multiple databases is to not use the pointer returned from the static addConnection method. You should use the connectionName argument:
https://doc.qt.io/qt-5/qsqldatabase.html#addDatabase-1 during initilization and query usage:
example:
void MyClass::initDb(QString dbPath, QString connName)
{
// initial db usage, etc
QSqlDatabase db = QSqlDatabase::addDatabase(YOUR_DRIVER, connName);
db.setDatabaseName(dbPath);
// open it, etc
}
void MyClass::updateThing(QString val, QString name, QString connName)
{
QString q = QString("UPDATE THINGS SET val=%1 WHERE name=%2").arg(val, name);
// add the reference to your database via the connection name
QSqlDatabase db = QSqlDatabase::database(connName);
QSqlQuery query(db);
query.exec(q);
// handle the query normally, etc
}

JDBC DriverManager trying to use non specified Oracle driver

I am trying to connect to an Oracle Database using the java.sql.DriverManager in a JSF application. I am using a Tomcat v7 with ojdbc5.jar.
I have a very simple sample project that consists of nothing else than this piece of java code:
String url = "jdbc:oracle:thin:#DBSERV:DBPORT:DBSID";
String user = "account_admin";
String password = "my_assword";
Connection connection = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
connection = DriverManager.getConnection(url, user, password);
connection.close();
} catch ..
...
Executed I get the following error:
java.lang.NoClassDefFoundError: Could not initialize class oracle.jdbc.driver.OracleDriver
Infact the class specified there "oracle.jdbc.driver.OracleDriver" is deprecated...and I can't change the Tomcat configuration. Therefore I specified "oracle.jdbc.OracleDriver" which loads just fine.
So the question is: Why does the DriverManager tries to load the "wrong" oracle driver, although I am loading another one?
I also tried as an alternative to Class.forName the following:
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
That does not change anything though. I also checked for the registered drivers in the following way:
Enumeration<Driver> driverList = DriverManager.getDrivers();
while(driverList.hasMoreElements()){
Driver driver = driverList.nextElement();
System.out.println(driver.getClass().toString());
}
The output:
class sun.jdbc.odbc.JdbcOdbcDriver
class oracle.jdbc.OracleDriver
So the desired driver seems to be registered, no trace of the deprecated "oracle.jdbc.driver.OracleDriver".
Thank you for any help
My problem just disappeared when I restarted the container. I can't explain why that is though.

Resources