如何从休眠提供者知道底层数据库名称 [英] How to know underlying database name from hibernate provider

查看:107
本文介绍了如何从休眠提供者知道底层数据库名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate 3.x和Jboss。
目前我们支持多个数据库。

现在在运行时,我怎么能知道底层的数据库信息?至少是名字还是数据库方言? (例如MySQL,Derby,Oracle等)?

任何人都可以通过任何方式找到这些信息吗?
我以为hibernate SessionFactory类会提供这样的api - 但它不是吗?


解决方案

非常感谢javamonkey79和costis来回答这个问题。



是 - 我可以阅读hibernate.properties/ cfg.xml文件 - 但我想避免文件读取工作流程。



看起来 Session :: connection() api现在已被弃用,但它仍然有效。
我们也可以通过其他方式检索相同的信息。



选项1



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



选项2



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

对于MySql,返回URL将采用以下形式:

  jdbc:mysql:// localhost:3306 / edm?useUnicode = true 


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,

解决方案

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

这篇关于如何从休眠提供者知道底层数据库名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆