Postgres connections not closing - Hibernate - postgresql

I am using PostgreSQL for my web application. I have put session.close in each Hibernate call in application code. But postgres connections are not closiing which makes the application to break after the connection count reahes the threshhold. I need to restart my postgres service to get it up again.
Using Apache Tomcat as webserver.
How can I solve this? Please suggest a permanent fix.
Code snippet below :.
session = sessionFactory.opensession();
Transaction tx= session.beginTransaction();
finally{
if(session != null) session.close();
}

Are you using the connection pooling or directly connecting through the Hibernate to database? If you are using it through connection pooling than please check the configuration over there once. And If you could post some piece of your code than it will be easy for us to debug and find the issue.

Related

Multiple server names in a single connection string

In the PostgreSQL documentation https://www.postgresql.org/docs/10/libpq-connect.html, it has been said that multiple hosts can be specified in a single connection string such that all the hosts will be tried in order one after the other until one of the server gets succeeds.
But when i tried to implement the same setting in the tag present in my ASP.net web.config file, it is throwing error as no such host name. I am using NpgSQL provider in order to connect to PostgreSQL database.
I need to add multiple server names in the connection string such that if the server#1 fails then it should try for the next server server#2 immediately provided in the order until it succeeds
Can you please suggest on how multiple hosts can be provided in the connection string?
The Npgsql driver does not currently support this functionality. The issue tracking this is https://github.com/npgsql/npgsql/issues/732, I'm still hoping we can get this into the next release but there's a lot going on.
Load balancing and failover is avaialble in Npgsql version 6. At the time of writing v.6 is in preview.
Simple failover example (server2 is only used if a connection could not be established to server1):
Host=server1,server2;Username=test;Password=test
Example with load balancing (round robin I guess):
Host=server1,server2,server3,server4,server5;Username=test;Password=test;Load
Balance Hosts=true;Target Session Attributes=prefer-standby
https://www.npgsql.org/doc/failover-and-load-balancing.html

How to establish the connection with MongoDB using jMeter with JSR223 sampler?

How to establish the connection with mongo dB using jmeter with JSR223 sampler? Whenever i am trying to establish connection it is failing without any response.I am suspecting this is due to auth mechanism.
Any help with necessary changes needs to be done on jmeter is much appreciated
Whenever you face an issue with your script always check jmeter.log file, it should normally contain the root cause or at least enough information to guess it.
If you're looking for a built-in JMeter way of load testing MongoDB you will need to add the next line to user.properties file:
not_in_menu
This way you will have MongoDB Source Config back and will be able to specify your MongoDB host, port and other connection parameters. Later in JSR223 Sampler you will be able to get db object like:
def db = MongoDBHolder.getDBFromSource('sourceName', 'databaseName')
or if you need to supply the credentials:
def db = MongoDBHolder.getDBFromSource('sourceName', 'databaseName', 'username', 'password')
More information: How to Load Test MongoDB with JMeter

Could not initialize class com.ibm.ws.ffdc.FFDCFilter. DSRA0010E: SQL State = 28P01, Error Code = 0

Can I get assistance with the error codes coming from eclipse when I try to deploy enterprise application on websphere. I followed craig st jean, I also face another problem with configuration i.e websphere data sources using postgresql. i am using a windows machine, 64bit arch. the error codes are the topic of this question. i hope this question can be seen as relevant, since not much solutions exist for the first issue concerning com.ibm.ws.ffdc.FFDCFilter, thus if one doesn't overcome the first, how can one press on and attempt to solve the second. thanks.
Webspere logs
The test connection operation failed for data source AppDb on server server1 at node Lenovo-PCNode01 with the following exception: java.sql.SQLException: FATAL: password authentication failed for user "listmanagerremote" DSRA0010E: SQL State = 28P01, Error Code = 0. View JVM logs for further details.
I have fixed the issues with deployment in the eclipse neon IDE. I think it is either as a result of the installation of the IBM WebSphere Application Server Traditional v8.0x Developer tools for Neon, and IBM jre.
Eclipse console final message
00000063 CompositionUn A WSVR0191I: Composition unit WebSphere:cuname=ListManager in BLA WebSphere:blaname=ListManager started.
Postgre documents the 28P01 SQL State as an invalid password:
"28P01 INVALID PASSWORD invalid_password"
https://www.postgresql.org/docs/9.0/static/errcodes-appendix.html
Check your data source configuration to ensure that you have specified the correct password, or if using an authentication alias for your data source, confirm that the authentication data configuration contains the correct password, and that you have configured the data source and/or resource reference to use that authentication data.

OrientDB and PostgreSQL JDBC drivers are clashing (InvocationTargetException): is there an OrientDB JAR with everything except JDBC?

My application uses both OrientDB and PostgreSQL databases for different purposes.
It seems they were able to coexist before, but today my code stopped working. Upon debugging, it seems that the OrientDB driver is attempting to connect to my PostgreSQL database when I'm expecting the PostgreSQL driver to connect instead.
Here is the sequence of events:
OrientDB connection is made (using OrientGraphFactory.setupPool()), transaction is started.
Connection attempt is made on PostgreSQL database, error occurs when trying to create the Connection object.
Here is the segment of code that creates the PostgreSQL connection:
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:postgresql://" + [...]);
return connection;
An InvocationTargetException is thrown at the DriverManager.getConnection() line. Here is the stack trace, clearly indicating that the OrientDB driver was the one trying to connect:
Error on opening database 'jdbc:postgresql://[hostname]/[db_name]'
com.orientechnologies.orient.core.exception.ODatabaseException: Error on opening database 'jdbc:postgresql://[hostname]/[db_name]'
at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.<init>(ODatabaseDocumentTx.java:204)
at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.<init>(ODatabaseDocumentTx.java:168)
at com.orientechnologies.orient.jdbc.OrientJdbcConnection.<init>(OrientJdbcConnection.java:62)
at com.orientechnologies.orient.jdbc.OrientJdbcDriver.connect(OrientJdbcDriver.java:52)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
Indeed it seems the JDBC drivers are clashing.
I don't actually need JDBC functionality with OrientDB in this case. However, I can't find the OrientDB JAR that doesn't contain JDBC. The home page lets you download JDBC-all or JDBC-only. Where can I find a JAR with all dependencies bundled into a single JAR, but without JDBC?
This is probably not caused by the drivers themselves but by the DriverManager getting "confused" which driver handles which URL.
You can bypass the DriverManager by asking the driver directly for a connection:
Driver drv = new org.postgresql.Driver();
Properties props = new Properties();
props.put("username", "foo_user");
props.put("password", "database_password");
Connection connection = drv.getConnection("jdbc:postgresql://dbhost/db_name", props);
Unrelated, but: Class.forName("org.postgresql.Driver"); is longer necessary with current Java and driver versions.
I solved the problem here:
https://github.com/orientechnologies/orientdb/commit/8e0f4bed41999cf68ae9de229b3ff6a4260813da
It was a misunderstanding on how the DriverManager registers drivers and then calls the getConnection method.
Solutions.
My suggestion is to not use the orientdb-jdbc-all jar at all. If you don't need to work with orient embedded in your app AND access to it via JDBC it is really too big.
Instead, use your dependency management framework (maven, gradle?) to import orient jars, maybe only the orientdb-client if you need to interact with a remote db, maybe more if you need to embed orient in your app.
If you need to interact to a remote Orient Server via JDBC, use only che orientdb-jdbc. But you need the fixed one, so you should build it from source, or wait for next 2.1.8 release.
If you want to stay with the jdbc fat jar, again you can build it from source right now, or you can wait next hotfix release (2.1.8).
hope this help,
best regards

Best way to store Database configuration parameters?

I'm developing a web application at the moment. The web application needs to access a Patients database, which for now is a simple MySQL database but may likely be replaced by some other DB (or data source) in the future. At the moment, everything is hardcoded but I would like to have some way to configure the DB connection (that is, the database URL, user, password etc.).
What would be a simple and straightforward solution? It would be good if I could change the configuration by simple editing of a file.
I've seen there's the Properties API as well as Preferences. Or is there some idiom concerning servlets/web apps?
A servlet is part of a web app, and this web app is deployed in a Java EE container (Tomcat, WebLogic, etc.).
The standard way to get a database connection is to use JNDI to get a DataSource instance, and to ask a connection to this DataSource. The DataSource, most of the time, will pool database connections to avoid creating and closing too many connections and thus be much faster :
Context initCtx = new InitialContext();
DataSource dataSource = (DataSource) initCtx.lookup("java:comp/env/jdbc/MyDataSource");
Connection c = dataSource.getConnection();
try {
// ...
}
finally {
c.close(); // makes the connection available for a new thread
}
The DataSource will have to be declared in the web.xml file:
<resource-ref>
<description>Datasource example</description>
<res-ref-name>jdbc/MyDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
It will have to be defined (with its URL, number of connections, user, password, settings, etc.) inside your Java EE container. This is where it depends on your container.
Read the following explanations for Tomcat : http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html
I think a configuration XML along with your web application is a good idea. Each time the application is initiated by a new request the configuration is loaded and the database connection information available from any internal context that you make.
On IIS this is a standard way through the Web.config file.
regards