Java 如何从休眠提供程序知道底层数据库名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4351443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know underlying database name from hibernate provider
提问by user530081
I am using hibernate 3.x with Jboss. Currently we support multiple databases.
我在 Jboss 中使用 hibernate 3.x。目前我们支持多个数据库。
Now in runtime, how can I know the underlying database information ? at least name or database-dialect? (e.g. MySQL, Derby, Oracle, etc)?
现在在运行时,我如何知道底层数据库信息?至少名称或数据库方言?(例如 MySQL、Derby、Oracle 等)?
Can any one suggests any way to find this information? I thought hibernate SessionFactory class will provide such api - but it is not?
任何人都可以建议任何方法来找到这些信息吗?我以为休眠 SessionFactory 类会提供这样的 api - 但它不是?
Thanks in advance,
提前致谢,
回答by Costis Aivalis
Since you have either a hibernate.properties or a hibernate.cfg.xml, you can allways read any information off these files.
由于您拥有 hibernate.properties 或 hibernate.cfg.xml,因此您始终可以从这些文件中读取任何信息。
回答by javamonkey79
I think you can do it this way:
我认为你可以这样做:
sessionFactory.getCurrentSession().connection().getMetaData().getURL()
回答by user530081
Thanks very much javamonkey79 and costis for responding to this question.
非常感谢 javamonkey79 和 costis 回答这个问题。
Yes - I can read the hibernate.properties/cfg.xml file - but I wanted to avoid the file reading workflow.
是的 - 我可以读取 hibernate.properties/cfg.xml 文件 - 但我想避免文件读取工作流程。
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.
现在看来Session::connection()
api 已被弃用,但它仍然有效。我们也可以用下面列出的另一种方式检索相同的信息。
OPTION 1
选项1
Session session = sessionFactory.openSession();
String dbURL = session.connection().getMetaData().getURL().toString();
session.close();
OPTION 2
选项 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:
对于 MySql,返回 URL 将采用以下形式:
jdbc:mysql://localhost:3306/edm?useUnicode=true
回答by user530081
sessionFactory.getDialect();
gives you the Dialect
and
给你Dialect
和
sessionFactory.getSettings().getConnectionProvider().getConnection().getMetaData().getURL();
provides you the connection url.
为您提供连接网址。
Note: This only works with the SessionFactoryImpl
class, not with the interface
注意:这只适用于SessionFactoryImpl
类,不适用于接口
回答by louis xie
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];
回答by Vito
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];
;)
;)
回答by Eagle_Eye
For Hibernate 5.2.0, to get the datasource..
对于 Hibernate 5.2.0,要获取数据源..
SessionFactoryImplementor entityManagerFactory = (SessionFactoryImplementor) sessionFactory.openSession().getEntityManagerFactory();
DatasourceConnectionProviderImpl connectionProvider = (DatasourceConnectionProviderImpl) entityManagerFactory.getServiceRegistry().getService(ConnectionProvider.class);
BasicDataSource dataSource = (BasicDataSource) connectionProvider.getDataSource();
dataSource.getUrl();