-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to get connection url?
PostPosted: Mon Apr 05, 2010 3:33 am 
Newbie

Joined: Mon Apr 05, 2010 3:19 am
Posts: 6
My customer want me to display the database connection url on top of every web page so that he can know whether he is connecting to test db server or live db server.
Is there any hibernate class, object or function that get connection url from hibernate.cfg.xml?

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: How to get connection url?
PostPosted: Mon Apr 05, 2010 9:59 am 
Newbie

Joined: Sun Jul 10, 2005 10:20 am
Posts: 17
Location: London
Code:
Connection connection = sessionFactory.getCurrentSession().connection();
DatabaseMetaData metaData = connection.getMetaData();
System.out.println(metaData.getURL());


However please note that the connection() method has been deprecated and it might not work in future versions.

Regards,

M.


Top
 Profile  
 
 Post subject: Re: How to get connection url?
PostPosted: Wed Apr 07, 2010 3:18 am 
Newbie

Joined: Mon Apr 05, 2010 3:19 am
Posts: 6
Thank you very much, the code works.
But I have to start transaction before I can get connection.


Top
 Profile  
 
 Post subject: Re: How to get connection url?
PostPosted: Wed Apr 07, 2010 5:33 pm 
Newbie

Joined: Sun Jul 10, 2005 10:20 am
Posts: 17
Location: London
You can substitute currentSession with the following:

Code:
Connection connection = sessionFactory.openSession().connection();


That should work without a transaction, although I can't see why you would use a database application without transactions.

Regards,

M.


Top
 Profile  
 
 Post subject: Re: How to get connection url?
PostPosted: Thu Apr 08, 2010 2:35 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi alzamabar,

I believe that woonjava's intention is to get the get the connection url from hibernate.cfg.xml
without establishing a connection to the database at all.
This makes quite sense, as the URL could be wrong or the database could be down.
In such case, how to show the connection url on the application's GUI?


Top
 Profile  
 
 Post subject: Re: How to get connection url?
PostPosted: Thu Apr 08, 2010 6:58 pm 
Newbie

Joined: Sun Jul 10, 2005 10:20 am
Posts: 17
Location: London
XML parsing and HTTP Application Context springs to mind. If an XML schema is not already available for the hibernate.cfg.xml one could be created and than a binding tool (e.g. JAXB) could be used to read the hibernate.cfg.xml into an object, set the Object in the HTTP Application/Session context and display its value. However how could the URL be wrong if taken from the connection? The requirement was to show the URL of the database connection on a page to check whether it was prod or test. I'd imagine that if the database was down than the page would either crash (worse scenario) or a user friendly message would be shown to the user (better scenario). So in either case taking the info from the database connection (which takes its values from hibernate.cfg.xml / META-INF/persistence.xml) seems the correct solution to me, but then, as always in IT there is no right or wrong, just different ways of doing things.

M.


Top
 Profile  
 
 Post subject: Re: How to get connection url?
PostPosted: Fri Apr 09, 2010 7:13 am 
Newbie

Joined: Tue Mar 23, 2010 9:04 am
Posts: 6
I think you can get the information through the settings object.
Code:
       EntityManagerFactoryImpl impl = (EntityManagerFactoryImpl)getEntityManagerFactory();
       SessionFactory sf = impl.getSessionFactory();
       Settings settings = ((SessionFactoryImpl)sf).getSettings();


Top
 Profile  
 
 Post subject: Re: How to get connection url?
PostPosted: Sun Apr 11, 2010 10:28 am 
Newbie

Joined: Sun Jul 10, 2005 10:20 am
Posts: 17
Location: London
FEB wrote:
I think you can get the information through the settings object.
Code:
       EntityManagerFactoryImpl impl = (EntityManagerFactoryImpl)getEntityManagerFactory();
       SessionFactory sf = impl.getSessionFactory();
       Settings settings = ((SessionFactoryImpl)sf).getSettings();


With the above solution you would still need to go through the Connection object, I think, as in the following snippet:

Code:
Settings settings = ((SessionFactoryImpl) sessionFactory).getSettings();
Assert.assertNotNull(settings);
Connection connection = settings.getConnectionProvider().getConnection();
System.err.println(connection.getMetaData().getURL());


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