Hibernate / DB Metadata

Background

In the early stages of learning Hibernate, I wanted to get some metadata about my database.

But, was lost as to how to do so.

Outline

  1. Connect to Database
  2. Get Session
    • Cast Session to sessionImpl
    • Get Connection from sessionImpl
    • Get metadata from Connection
      • Access metadata
        • getURL
        • getDriverName
        • getDatabaseProductName
        • getDatabaseProductVersion
        • getUserName
        • getDefaultTransactionIsolation

Code

void getDBMetadata(Session session)
    throws Exception
    {

        SessionImpl sessionImpl;
        Connection conn;
        DatabaseMetaData dbMetadata;

        int iDefaultTransactionIsolation;
        String strDefaultTransactionIsolation = null;

        //get Session Implementation
        sessionImpl = (SessionImpl) session;

        //Get Session Implementation Connection
        conn = sessionImpl.connection();

        //Get Metadata
        dbMetadata = conn.getMetaData();

        System.out.println
        (
              "Database URL :- "
            + dbMetadata.getURL()
        );

        System.out.println
            (
                  "Driver Name :- "
                + dbMetadata.getDriverName()
            );

        System.out.println
        ("Database Product Name :- "
            + dbMetadata.getDatabaseProductName()
        );

        System.out.println
            (
                "Database Product Version :- "
                        + dbMetadata.getDatabaseProductVersion()
            );

        System.out.println
        (
            "Database Username:- "
                + dbMetadata.getUserName()
        );          

        iDefaultTransactionIsolation
        	= dbMetadata.getDefaultTransactionIsolation();

        strDefaultTransactionIsolation
        	= getDefaultTransactionIsolationAsString
        	  (
    			  iDefaultTransactionIsolation
			  );

        System.out.println
        (
            "Default Isolation Level ( as int ):- "
                    + dbMetadata.getDefaultTransactionIsolation()
        );  

        if (strDefaultTransactionIsolation != null)
        {

	        System.out.println
	        (
	            "Default Isolation Level ( as String ) :- "
	                    + strDefaultTransactionIsolation
	        );
        }

    }

 

    String getDefaultTransactionIsolationAsString(int value)
    {

    	String strValue = null;

    	switch(value)
    	{
    	   case java.sql.Connection.TRANSACTION_NONE:
    		  strValue = "None";
    	      break; 

    	   case java.sql.Connection.TRANSACTION_READ_COMMITTED :
    		  strValue = "Read Committed";
    	      break; 

    	   case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED :
     		  strValue = "Read UnCommitted";
     	      break; 

    	   case java.sql.Connection.TRANSACTION_REPEATABLE_READ :
      		  strValue = "Repeatable Read";
      	      break; 

    	   case java.sql.Connection.TRANSACTION_SERIALIZABLE :
       		  strValue = "Serializable";
       	      break; 

    	   default :
    	      strValue = null;
    	}

    	return (strValue);

    }

Output

Output – Text


Database URL :- jdbc:sqlserver://localhost:1433;useBulkCopyForBatchInsert=false;cancelQueryTimeout=-1;sslProtocol=TLS;jaasConfigurationName=SQLJDBCDriver;statementPoolingCacheSize=0;serverPreparedStatementDiscardThreshold=10;enablePrepareOnFirstPreparedStatementCall=false;fips=false;socketTimeout=0;authentication=NotSpecified;authenticationScheme=nativeAuthentication;xopenStates=false;sendTimeAsDatetime=true;trustStoreType=JKS;trustServerCertificate=false;TransparentNetworkIPResolution=true;serverNameAsACE=false;sendStringParametersAsUnicode=true;selectMethod=direct;responseBuffering=adaptive;queryTimeout=-1;packetSize=8000;multiSubnetFailover=false;loginTimeout=15;lockTimeout=-1;lastUpdateCount=true;encrypt=false;disableStatementPooling=true;databaseName=sakila;columnEncryptionSetting=Disabled;applicationName=Microsoft JDBC Driver for SQL Server;applicationIntent=readwrite;
Driver Name :- Microsoft JDBC Driver 7.0 for SQL Server
Database Product Name :- Microsoft SQL Server
Database Product Version :- 14.00.3045
Database Username:- sakila
Default Isolation Level ( as int ):- 2
Default Isolation Level ( as String ) :- Read Committed

Output – Image

 

Source Code Control

GitHub

DanielAdeniji/HibernateDBMetadata
Link

 

References

  1. Oracle.com
    • java.sql
      • DatabaseMetaData
      • Connection
    • API
      • Constant Field Values
    • Sun Java System Application Server Platform Edition 8.2 Developer’s Guide
      • Using JDBC Transaction Isolation Levels
  2. jboss.org
    • org.hibernate
      • org.hibernate.impl