Java Code Examples for org.hibernate.Session.connection()

The following are Jave code examples for showing how to use connection() of the org.hibernate.Session class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: alfresco-core   File: DialectFactoryBean.java   View Source Code Vote up 5 votes
@SuppressWarnings("deprecation")
@Override
public Dialect getObject() throws SQLException
{
    Session session = ((SessionFactory) this.localSessionFactory.getObject()).openSession();
    Configuration cfg = this.localSessionFactory.getConfiguration();
    Connection con = null;
    try
    {
        // make sure that we AUTO-COMMIT
        con = session.connection();
        con.setAutoCommit(true);
        DatabaseMetaData meta = con.getMetaData();

        overrideDialectPropertyForDriver(cfg.getProperties(), meta.getDriverName());

        Dialect dialect = DialectFactory.buildDialect(cfg.getProperties(), meta.getDatabaseProductName(), meta.getDatabaseMajorVersion());

        dialect = changeDialect(cfg, dialect);
        return dialect;
    }
    finally
    {
        try
        {
            con.close();
        }
        catch (Exception e)
        {
        }
    }
}
 
Example 2
Project: lams   File: HibernateTransactionManager.java   View Source Code Vote up 4 votes
@Override
@SuppressWarnings("deprecation")
protected void doCleanupAfterCompletion(Object transaction) {
	HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;

	// Remove the session holder from the thread.
	if (txObject.isNewSessionHolder()) {
		TransactionSynchronizationManager.unbindResource(getSessionFactory());
	}

	// Remove the JDBC connection holder from the thread, if exposed.
	if (getDataSource() != null) {
		TransactionSynchronizationManager.unbindResource(getDataSource());
	}

	Session session = txObject.getSessionHolder().getSession();
	if (this.prepareConnection && session.isConnected() && isSameConnectionForEntireSession(session)) {
		// We're running with connection release mode "on_close": We're able to reset
		// the isolation level and/or read-only flag of the JDBC Connection here.
		// Else, we need to rely on the connection pool to perform proper cleanup.
		try {
			Connection con = session.connection();
			DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel());
		}
		catch (HibernateException ex) {
			logger.debug("Could not access JDBC Connection of Hibernate Session", ex);
		}
	}

	if (txObject.isNewSession()) {
		if (logger.isDebugEnabled()) {
			logger.debug("Closing Hibernate Session [" + SessionFactoryUtils.toString(session) +
					"] after transaction");
		}
		SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug("Not closing pre-bound Hibernate Session [" +
					SessionFactoryUtils.toString(session) + "] after transaction");
		}
		if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
			session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
		}
		if (!this.hibernateManagedSession) {
			session.disconnect();
		}
	}
	txObject.getSessionHolder().clear();
}