13

I am using hibernate 3.x with Jboss. Currently we support multiple databases.

Now in runtime, how can I know the underlying database information ? at least name or database-dialect? (e.g. MySQL, Derby, Oracle, etc)?

Can any one suggests any way to find this information? I thought hibernate SessionFactory class will provide such api - but it is not?

Thanks in advance,

javamonkey79
  • 17,443
  • 36
  • 114
  • 172
user530081
  • 211
  • 1
  • 2
  • 6
  • Possible duplicate: http://stackoverflow.com/questions/821466/how-can-i-get-the-database-name-i-am-connected-to-through-hibernate – javamonkey79 Dec 04 '10 at 01:55

11 Answers11

9

I think you can do it this way:

sessionFactory.getCurrentSession().connection().getMetaData().getURL()
javamonkey79
  • 17,443
  • 36
  • 114
  • 172
8

Thanks very much javamonkey79 and costis for responding to this question.

Yes - I can read the hibernate.properties/cfg.xml file - but I wanted to avoid the file reading workflow.

It appears that Session::connection() api is deprecated now, but it still works. We also can retrieve the same info in another way as listed below.

OPTION 1

Session session = sessionFactory.openSession();
String dbURL = session.connection().getMetaData().getURL().toString();
session.close();

OPTION 2

Settings settings = ((SessionFactoryImpl) sessionFactory).getSettings();
if (settings != null) {
    Connection connection = settings.getConnectionProvider().getConnection();
    String dbURL  = connection.getMetaData().getURL();
    connection.close();
}

For MySql, the return URL will be in form of:

jdbc:mysql://localhost:3306/edm?useUnicode=true
mik01aj
  • 11,928
  • 15
  • 76
  • 119
user530081
  • 211
  • 1
  • 2
  • 6
4
sessionFactory.getDialect();

gives you the Dialect and

sessionFactory.getSettings().getConnectionProvider().getConnection().getMetaData().getURL();

provides you the connection url.

Note: This only works with the SessionFactoryImpl class, not with the interface

3

Easiest way is to get the currently configured properties and then get your db URL using key "hibernate.connection.url". The following code prints current session url

System.out.println(sessionFactory.getProperties().get("hibernate.connection.url"))

on my system I get

jdbc:postgresql://localhost:15432/tempdb
renuka28
  • 31
  • 1
3

Or you can try:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;
Properties props = sessionFactoryImpl.getProperties();
String url = props.get("hibernate.connection.url").toString();
String[] urlArray = url.split(":");
String db_name = urlArray[urlArray.length - 1];
louis xie
  • 1,352
  • 2
  • 12
  • 22
  • I was hoping this would be a way I could look at all Hibernate properties, but it seems that it only works if the config is being read from the file hibernate.cfg.xml. I am using Spring to configure Hibernate so it doesn't work. Any idea how to access the SessionFactoryImpl that is created by Spring? – Glenn Lawrence Apr 12 '15 at 04:21
  • 1
    I figured out that I could get the SessionFactory from Spring like this `(SessionFactoryImpl) appContext.getBean("sessionFactory");` where appContext is my ApplicationContext. Using this I can get lots of properties, but oddly not the *hibernate.connection.url* mentioned in this post. – Glenn Lawrence Apr 12 '15 at 04:47
1

and you can try this code:

String url = null;
try {
    url = getSession().connection().getMetaData().getURL().toString();
} catch (HibernateException e) {
    e.printStackTrace();
} catch (SQLException e) {
    e.printStackTrace();
}
String[] urlArray = url.split(":");
String db_name = urlArray[1];

;)

Vito
  • 1,080
  • 9
  • 19
1

Since you have either a hibernate.properties or a hibernate.cfg.xml, you can allways read any information off these files.

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
1

For Hibernate 5.2.0, to get the datasource..

SessionFactoryImplementor entityManagerFactory = (SessionFactoryImplementor) sessionFactory.openSession().getEntityManagerFactory();
DatasourceConnectionProviderImpl connectionProvider = (DatasourceConnectionProviderImpl) entityManagerFactory.getServiceRegistry().getService(ConnectionProvider.class);
BasicDataSource dataSource = (BasicDataSource) connectionProvider.getDataSource();
dataSource.getUrl();
Eagle_Eye
  • 1,044
  • 1
  • 14
  • 26
0

You can inspect it in variable view in IntelliJ as below

Screenshot

phray2002
  • 425
  • 4
  • 9
0

this worked for me:

DataSource dataSource = (DataSource)entityManagerFactory.getProperties()
.get("hibernate.connection.datasource");

Connection connection = dataSource.getConnection();
String databaseName = connection.getCatalog();  
0

With Hibernate5 and C3P0 managed connection pool, the following returned me the JDBC URL:

    ((DriverManagerDataSource) 
            ((WrapperConnectionPoolDataSource) 
            ((ComboPooledDataSource) hibernateTemplate.getSessionFactory().getProperties()
                    .get("hibernate.connection.datasource"))
            .getConnectionPoolDataSource())
            .getNestedDataSource()).getJdbcUrl();
James Jithin
  • 10,183
  • 5
  • 36
  • 51