25

Is there a way to get the hostname from the DataSource class in java? I mean, I have a DataSource object (DS), which is annotated to get the Jboss datasource. Anyway, I want to get the hostname used in that DS.

Debugging, i can see it this way: I get the Connection from DS, then I get the DataSourceMetaData and inside of that the is something called Protocol Connection which have the hostname, but I don't know how to get it.

Anyone here knows how? or another way to get the hostname? Thanks in advance. Kind regards, RDAM

Ron
  • 2,115
  • 3
  • 21
  • 29

1 Answers1

50

Once you have the DatabaseMetaData, just call the getURL() method which should contain the hostname like so:

dataSource.getConnection().getMetaData().getURL();
Suraj Chandran
  • 23,444
  • 11
  • 58
  • 92
  • 1
    it contains the hostname but in a jdbc URL String format, which is not the way to get it. Is a bad practice to depend on parsing a database URL to get a hostname. What if the URL has no hostname? What if you use different databases? URLs are in different format. That's why I'm looking the hostname directly – Ron Jun 14 '11 at 08:07
  • 1
    i doubt whether u can..the reason is that the both DataSource and Connection are abstraction that dont know(dont need to know) the concept of hostname. All it knows is how to get a connection and how to call queries – Suraj Chandran Jun 14 '11 at 08:09
  • Yes they are meant into that concept. But picture my problem. I'm running -pg_dumps- (Many performance reasons to do that but that's other subject), but the jboss is not always in the same machine as the databaServer, so I need to pass the hostname parameter, and using the DS is the best way I think. Cause you know for sure where the database is – Ron Jun 14 '11 at 08:15
  • 1
    @RDAM, i am not sure i get what u mean. why not just have the db details configured in files, where everyone can read it. we even had a condition where all confiugrations of all DBs where stored in a central DB – Suraj Chandran Jun 14 '11 at 08:25
  • Great! I have configuration parameters inside a database. I never pictured that as another configuration parameter #Mybad. Thank u very much for your help @Suraj – Ron Jun 14 '11 at 08:33
  • @SurajChandran mate, as to "both DataSource and Connection are abstraction that dont know(dont need to know) the concept of hostname" - if a Connection does not need to know the concept of hostname then I don't know what does... otherwise you're quite right and thanks for the helpful answer! – Peter Perháč Jul 15 '12 at 19:51
  • 3
    The real problem is that the metadata is not available unless the connection is successful. If getConnection() returns null there is no metadata to be gotten and an exception will be thrown. So the question still stands is there anyway to get the innards of the dataSourceProperties table that is a part of the datasource object? I can see it in the debugger - just can't get to it from any class public methods. – Nelda.techspiress Mar 21 '16 at 17:44
  • Also, the hostname might not be even a concept depending on the socket factory. For example for CloudSQL Postgres you would typically pass the database instance as the data source property "cloudSqlInstance", so you wouldn't be able to obtain this information from the url itself. – andresp Jan 26 '21 at 15:03