-->
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.  [ 6 posts ] 
Author Message
 Post subject: How can I get database name or JDBC connection string?
PostPosted: Thu Jun 21, 2007 7:16 am 
Beginner
Beginner

Joined: Wed Sep 06, 2006 12:08 pm
Posts: 48
Location: Warsaw - Poland - Europe - Milky Way
Hi Everybody

I’m using: Hibernate Core 3.2.4.sp1
My system is a multithreaded standalone Java 5 application with c3p0 connection pool.

How can I get database name or JDBC connection string?

You can ask, why, the hell, I need database name? I need this name to be absolutely sure that my TestCases are not running on production environments.

Kind regards,
Adam


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 4:37 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Can you post the question in forum specified format.

Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 6:06 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Its possible using a lot of reflection api.

Session object has a private "jdbcContext" object which holds the actual connection object. You will need to get hold of this connection object and from that find out what are the connection parameters being used.

*Please do rate if this helps*

Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 25, 2007 5:28 am 
Beginner
Beginner

Joined: Wed Sep 06, 2006 12:08 pm
Posts: 48
Location: Warsaw - Poland - Europe - Milky Way
Hello Jitendra

Reflection is rather not a nice solution here :>

Finally I have found a solution. Piece of code:

Code:
final SessionFactoryImplementor sessionFactoryImplementor =
(SessionFactoryImplementor) HibernateUtil.getSessionFactory();
      
final Settings settings = sessionFactoryImplementor.getSettings();

ConnectionProvider connectionProvider = settings.getConnectionProvider();

Connection connection = connectionProvider.getConnection();
DatabaseMetaData databaseMetaData = connection.getMetaData();
String url = databaseMetaData.getURL();


And url has a following format:
jdbc:oracle:thin:@chmiel.unx.xxx.yy:1521:MYDATABASENAME

btw:
Thanks for your replies, Jitendra!

Kind regards,
Adam


Top
 Profile  
 
 Post subject: Re: How can I get database name or JDBC connection string?
PostPosted: Mon Feb 14, 2011 1:53 pm 
Newbie

Joined: Mon Feb 14, 2011 1:26 pm
Posts: 1
I do know this is an old post, but is the first result in Google, and IMHO there's a better and flexible way to do the trick above

Guys,

Using the same object mentioned above ( SessionFactoryImplementor) i'm able to get the current database (catalog) name from hibernate, without needing to strip the connection url, which can be painful if you have many connections strings.
Code:
      final SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) HibernateUtil.getSessionFactory();
      Dialect dialect = sessionFactoryImplementor.getSettings().getDialect();
      String catalog = sessionFactoryImplementor.getSettings().getConnectionProvider().getConnection().getCatalog();
      
      
      System.out.println( "Dialect:" +  dialect.getClass().getSimpleName() );
      System.out.println( "Catalog: " + catalog );


In the snippet above, catalog is what you are looking for.

- Felipe


Top
 Profile  
 
 Post subject: Re: How can I get database name or JDBC connection string?
PostPosted: Wed Feb 16, 2011 5:43 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
@felipe.nascimento:

your proposed code forgets to close the connection, better is:

Code:
Connection conn =  sessionFactoryImplementor.getSettings().getConnectionProvider().getConnection();
String catalog =conn.getCatalog();
conn.close();
...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.