I have an app which is using hibernate and jpa. I need to find out which db it is connected to so that some native sql query i execute based on db say for eg. oracle and postgre
if anybody else (like me) wants to try to get some jdbc information and you are using hibernate 4.x, you might try it that way:
import java.sql.Connection;
import java.sql.SQLException;
import org.hibernate.jdbc.Work;
public class ConnectionInfo implements Work {
public String dataBaseUrl;
public String dataBaseProductName;
public String driverName;
@Override
public void execute(Connection connection) throws SQLException {
dataBaseUrl = connection.getMetaData().getURL();
dataBaseProductName = connection.getMetaData().getDatabaseProductName();
driverName = connection.getMetaData().getDriverName();
}
public String getDataBaseProductName() {
return dataBaseProductName;
}
public void setDataBaseProductName(String dataBaseProductName) {
this.dataBaseProductName = dataBaseProductName;
}
public String getDataBaseUrl() {
return dataBaseUrl;
}
public void setDataBaseUrl(String dataBaseUrl) {
this.dataBaseUrl = dataBaseUrl;
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
}
Now you can retrieve your information like that:
// -- snip
org.hibernate.ejb.EntityManagerImpl entityManagerImpl = (org.hibernate.ejb.EntityManagerImpl) genericDBAccess.getEntityManager().getDelegate();
Session session = entityManagerImpl.getSession();
connectionInfo = new ConnectionInfo();
session.doWork(connectionInfo);
// -- snap
Hope that helps! I drove crazy finding this information....