These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Determine database name at runtime
PostPosted: Tue May 04, 2010 5:58 am 
Newbie

Joined: Fri May 25, 2007 10:38 pm
Posts: 6
Hey Guys,

I would like to determine the database name at runtime. It is configured in hibernate.cfg.xml and used to connect with the db. It would be helpful and I could avoid copy&paste :)

Thanks in advance.

Kon


Top
 Profile  
 
 Post subject: Re: Determine database name at runtime
PostPosted: Fri May 07, 2010 4:42 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Once you have initialized the hiberante sessionfactory you should be able to obtain the database name from the
connection URL like this:

Code:
Settings settings = ((SessionFactoryImpl) sessionFactory).getSettings();
java.sql.Connection connection = settings.getConnectionProvider().getConnection();
System.err.println(connection.getMetaData().getURL());
connection.close(); // important in order to free resources


Top
 Profile  
 
 Post subject: Re: Determine database name at runtime
PostPosted: Mon May 10, 2010 9:17 am 
Newbie

Joined: Fri May 25, 2007 10:38 pm
Posts: 6
Hey pb00067,

thanks for the hint. I'm using following code to extract the name:

Code:
String URL = connection.getMetaData().getURL();
connection.close(); // important in order to free resources
dbName = URL.substring(URL.lastIndexOf('/') + 1, URL.length());


Do you think, there might be a more reliable solution? Thanks.

Kon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.