org.springframework.orm.hibernate4.SessionFactoryUtils Java Examples

The following examples show how to use org.springframework.orm.hibernate4.SessionFactoryUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpenSessionInterceptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
	SessionFactory sf = getSessionFactory();
	if (!TransactionSynchronizationManager.hasResource(sf)) {
		// New Session to be bound for the current method's scope...
		Session session = openSession();
		try {
			TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
			return invocation.proceed();
		}
		finally {
			SessionFactoryUtils.closeSession(session);
			TransactionSynchronizationManager.unbindResource(sf);
		}
	}
	else {
		// Pre-bound Session found -> simply proceed.
		return invocation.proceed();
	}
}
 
Example #2
Source File: OpenSessionInterceptor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
	SessionFactory sf = getSessionFactory();
	if (!TransactionSynchronizationManager.hasResource(sf)) {
		// New Session to be bound for the current method's scope...
		Session session = openSession();
		try {
			TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
			return invocation.proceed();
		}
		finally {
			SessionFactoryUtils.closeSession(session);
			TransactionSynchronizationManager.unbindResource(sf);
		}
	}
	else {
		// Pre-bound Session found -> simply proceed.
		return invocation.proceed();
	}
}
 
Example #3
Source File: DbTableProcess.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/**
 * 获取数据库中列的描述
 * @param tableName
 * @param session
 * @return
 * @throws SQLException
 */
public static Map<String, ColumnMeta> getColumnMetadataFormDataBase(String schemaName, String tableName, Session session) throws SQLException{
	Connection conn = SessionFactoryUtils.getDataSource(session.getSessionFactory()).getConnection();
	DatabaseMetaData dbMetaData = conn.getMetaData();
	ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%");	
	ColumnMeta columnMeta;
	Map<String, ColumnMeta> columnMap = new HashMap<String, ColumnMeta>();
	while (rs.next()){
		columnMeta = new ColumnMeta();
		columnMeta.setTableName(rs.getString("COLUMN_NAME").toLowerCase());
		columnMeta.setColumnName(rs.getString("COLUMN_NAME").toLowerCase());
		columnMeta.setColunmType(dbTableHandle.getMatchClassTypeByDataType(rs.getString("TYPE_NAME"),rs.getInt("DECIMAL_DIGITS")));
		columnMeta.setColumnSize(rs.getInt("COLUMN_SIZE"));
		columnMeta.setDecimalDigits(rs.getInt("DECIMAL_DIGITS"));
		columnMeta.setIsNullable(rs.getInt("NULLABLE")==1?"Y":"N");
		columnMeta.setComment(rs.getString("REMARKS"));
		columnMeta.setFieldDefault(judgeIsNumber(rs.getString("COLUMN_DEF"))==null?"":judgeIsNumber(rs.getString("COLUMN_DEF")));
		logger.info("getColumnMetadataFormDataBase --->COLUMN_NAME:"+rs.getString("COLUMN_NAME")+" TYPE_NAME :"+rs.getString("TYPE_NAME")
				+" DECIMAL_DIGITS:"+rs.getInt("DECIMAL_DIGITS")+" COLUMN_SIZE:"+rs.getInt("COLUMN_SIZE"));
		columnMap.put(rs.getString("COLUMN_NAME").toLowerCase(), columnMeta);
	}
	
	return columnMap;
}
 
Example #4
Source File: SpringSessionSynchronization.java    From lemon with Apache License 2.0 6 votes vote down vote up
public void beforeCommit(boolean readOnly) throws DataAccessException {
    if (!readOnly) {
        Session session = getCurrentSession();

        // Read-write transaction -> flush the Hibernate Session.
        // Further check: only flush when not FlushMode.MANUAL.
        if (!FlushMode.isManualFlushMode(session.getFlushMode())) {
            try {
                logger.debug("Flushing Hibernate Session on transaction synchronization");
                session.flush();
            } catch (HibernateException ex) {
                throw SessionFactoryUtils
                        .convertHibernateAccessException(ex);
            }
        }
    }
}
 
Example #5
Source File: OpenSessionInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unbind the Hibernate {@code Session} from the thread and close it).
 * @see org.springframework.transaction.support.TransactionSynchronizationManager
 */
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
	if (!decrementParticipateCount(request)) {
		SessionHolder sessionHolder =
				(SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
		logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor");
		SessionFactoryUtils.closeSession(sessionHolder.getSession());
	}
}
 
Example #6
Source File: OpenSessionInViewInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Unbind the Hibernate {@code Session} from the thread and close it).
 * @see org.springframework.transaction.support.TransactionSynchronizationManager
 */
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
	if (!decrementParticipateCount(request)) {
		SessionHolder sessionHolder =
				(SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
		logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor");
		SessionFactoryUtils.closeSession(sessionHolder.getSession());

	}
}
 
Example #7
Source File: SpringSessionSynchronization.java    From lemon with Apache License 2.0 5 votes vote down vote up
public void flush() {
    try {
        logger.debug("Flushing Hibernate Session on explicit request");
        getCurrentSession().flush();
    } catch (HibernateException ex) {
        throw SessionFactoryUtils.convertHibernateAccessException(ex);
    }
}
 
Example #8
Source File: AsyncRequestInterceptor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void closeAfterTimeout() {
	if (this.timeoutInProgress) {
		logger.debug("Closing Hibernate Session after async request timeout");
		SessionFactoryUtils.closeSession(this.sessionHolder.getSession());
	}
}
 
Example #9
Source File: AsyncRequestInterceptor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void closeAfterTimeout() {
	if (this.timeoutInProgress) {
		logger.debug("Closing Hibernate Session after async request timeout");
		SessionFactoryUtils.closeSession(this.sessionHolder.getSession());
	}
}
 
Example #10
Source File: SpringSessionSynchronization.java    From lemon with Apache License 2.0 4 votes vote down vote up
public int getOrder() {
    return SessionFactoryUtils.SESSION_SYNCHRONIZATION_ORDER;
}
 
Example #11
Source File: DbTableProcess.java    From jeecg with Apache License 2.0 4 votes vote down vote up
/**
 * 获取数据库中列的描述
 * @param tableName
 * @param session
 * @return
 * @throws SQLException
 */
public static Map<String, ColumnMeta> getColumnMetadataFormDataBase(String schemaName, String tableName, Session session) throws SQLException{
	Connection conn = null;

	try {
		conn = SessionFactoryUtils.getDataSource(session.getSessionFactory()).getConnection();
	} catch (Exception e) {
		logger.error(e);
		e.printStackTrace();
	}
	
	DatabaseMetaData dbMetaData = conn.getMetaData();
	ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%");	
	ColumnMeta columnMeta;
	Map<String, ColumnMeta> columnMap = new HashMap<String, ColumnMeta>();
	while (rs.next()){

		columnMeta = new ColumnMeta();
		columnMeta.setTableName(tableName);
		String columnName = rs.getString("COLUMN_NAME").toLowerCase();
		columnMeta.setColumnName(columnName);
		String typeName = rs.getString("TYPE_NAME");
		int decimalDigits = rs.getInt("DECIMAL_DIGITS");
		String colunmType = dbTableHandle.getMatchClassTypeByDataType(typeName,decimalDigits);
		columnMeta.setColunmType(colunmType);
		int columnSize = rs.getInt("COLUMN_SIZE");
		columnMeta.setColumnSize(columnSize);
		columnMeta.setDecimalDigits(decimalDigits);
		String isNullable = rs.getInt("NULLABLE")==1?"Y":"N";
		columnMeta.setIsNullable(isNullable);
		String comment = rs.getString("REMARKS");
		columnMeta.setComment(comment);
		String columnDef = rs.getString("COLUMN_DEF");
		String fieldDefault = judgeIsNumber(columnDef)==null?"":judgeIsNumber(columnDef);
		columnMeta.setFieldDefault(fieldDefault);
		logger.info("getColumnMetadataFormDataBase --->COLUMN_NAME:"+columnName.toUpperCase()+" TYPE_NAME :"+typeName
				+" DECIMAL_DIGITS:"+decimalDigits+" COLUMN_SIZE:"+columnSize);
		columnMap.put(columnName, columnMeta);
		/*columnMeta.setTableName(rs.getString("COLUMN_NAME").toLowerCase());
		columnMeta.setColumnName(rs.getString("COLUMN_NAME").toLowerCase());
		columnMeta.setColunmType(dbTableHandle.getMatchClassTypeByDataType(rs.getString("TYPE_NAME"),rs.getInt("DECIMAL_DIGITS")));
		columnMeta.setColumnSize(rs.getInt("COLUMN_SIZE"));
		columnMeta.setDecimalDigits(rs.getInt("DECIMAL_DIGITS"));
		columnMeta.setIsNullable(rs.getInt("NULLABLE")==1?"Y":"N");
		columnMeta.setComment(rs.getString("REMARKS"));
		columnMeta.setFieldDefault(judgeIsNumber(rs.getString("COLUMN_DEF"))==null?"":judgeIsNumber(rs.getString("COLUMN_DEF")));
		logger.info("getColumnMetadataFormDataBase --->COLUMN_NAME:"+rs.getString("COLUMN_NAME")+" TYPE_NAME :"+rs.getString("TYPE_NAME")
				+" DECIMAL_DIGITS:"+rs.getInt("DECIMAL_DIGITS")+" COLUMN_SIZE:"+rs.getInt("COLUMN_SIZE"));
		columnMap.put(rs.getString("COLUMN_NAME").toLowerCase(), columnMeta);*/
	}
	
	return columnMap;
}