Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know database type programmatically in JPA?

Is there possible way to know which database server is use in JPA, programmatically? I think, there is a way, otherwise, JPA cannot transform JPQL to native query at runtime. My program also need to know DB Server Type. Is there any function for that feature?

I use eclipseLink & jpa 2.0

My expected program is :

EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA");
String dbPlatform = emf.getDBPlatform();
System.out.println(dbPlatform);

Output :

Oracle
MySql
MSSql
DB2
....
like image 801
Zaw Than oo Avatar asked Dec 17 '13 08:12

Zaw Than oo


1 Answers

I never needed it, but I think the way to go is to get the JDBC connection and find in its properties that piece of information. In particular getClientInfo() and getMetaData() might be interesting.

This could be implementation specific, but as you said you use EclipseLink, you can use the following:

java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class);

Reference from here.

PS: I think you want to solve an intersting problem.

like image 168
V G Avatar answered Oct 17 '22 01:10

V G