Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display application name in Session Information when connecting via JDBC

I create a connection to a postgres 9 database using the stardard JDBC driver.

...
Connection myCon = DriverManager.getConnection("jdbc:postgresql://localhost/test?&user=test&password=test"); 
...

When I check the server status with PgAdmin and display all database sessions, I can see that the "Application Name" is not set for my Session. Is there a way to set the application name in the JDBC connection?

like image 676
markus Avatar asked Aug 22 '11 10:08

markus


People also ask

Which statement is used to obtain a connection in JDBC?

JDBC API `Statement` is used to execute SQL queries in the database. We can create the Statement object by calling Connection `createStatement()` method. We can use Statement to execute static SQL queries by passing query through different execute methods such as execute(), executeQuery(), executeUpdate() etc.

What is data source name in JDBC?

JDBC data sources include options that determine the identity of the data source, way the data is handled on a database connection, and the way transactions are handled when a connection from the data source is used in a global transaction.

Which class is used to connect Java application to JDBC?

The getConnection() method of DriverManager class is used to establish connection with the database.


1 Answers

That is possible to set application name as connection parameter since Postgres JDBC 9.1dev-900:

Add support for setting application_name on both connection startup and later through Connection.setClientInfo. (jurka)

ex: jdbc:postgresql://localhost:5432/DbName?ApplicationName=MyApp

or Connection.setClientInfo("ApplicationName", "My App")

For previous versions you could do this by setting application_name runtime parameter:

s.execute("SET application_name TO 'MyApp'");
like image 151
Grzegorz Szpetkowski Avatar answered Oct 06 '22 00:10

Grzegorz Szpetkowski