开发者

How to know underlying database name from hibernate provider

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 thoug开发者_JS百科ht hibernate SessionFactory class will provide such api - but it is not?

Thanks in advance,


I think you can do it this way:

sessionFactory.getCurrentSession().connection().getMetaData().getURL()


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


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


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];


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


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


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];

;)


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();


You can inspect it in variable view in IntelliJ as below

How to know underlying database name from hibernate provider


this worked for me:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜