/// <summary> /// Begins the <see cref="DbTransaction"/> on the <see cref="DbConnection"/> /// used by the <see cref="ISession"/>. /// </summary> /// <exception cref="TransactionException"> /// Thrown if there is any problems encountered while trying to create /// the <see cref="DbTransaction"/>. /// </exception> public void Begin(IsolationLevel isolationLevel) { using (session.BeginProcess()) { if (begun) { return; } if (commitFailed) { throw new TransactionException("Cannot restart transaction after failed commit"); } if (isolationLevel == IsolationLevel.Unspecified) { isolationLevel = session.Factory.Settings.IsolationLevel; } log.Debug("Begin ({0})", isolationLevel); try { if (isolationLevel == IsolationLevel.Unspecified) { trans = session.Connection.BeginTransaction(); } else { trans = session.Connection.BeginTransaction(isolationLevel); } } catch (HibernateException) { // Don't wrap HibernateExceptions throw; } catch (Exception e) { log.Error(e, "Begin transaction failed"); throw new TransactionException("Begin failed with SQL exception", e); } begun = true; committed = false; rolledBack = false; session.AfterTransactionBegin(this); foreach (var dependentSession in session.ConnectionManager.DependentSessions) { dependentSession.AfterTransactionBegin(this); } } }
/// <summary> /// Return the query results of all the queries /// </summary> public IList List() { using (session.BeginProcess()) { bool cacheable = session.Factory.Settings.IsQueryCacheEnabled && isCacheable; combinedParameters = CreateCombinedQueryParameters(); if (log.IsDebugEnabled()) { log.Debug("Multi query with {0} queries.", queries.Count); for (int i = 0; i < queries.Count; i++) { log.Debug("Query #{0}: {1}", i, queries[i]); } } try { Before(); var querySpaces = new HashSet <string>(Translators.SelectMany(t => t.QuerySpaces)); if (resultSetsCommand.HasQueries) { session.AutoFlushIfRequired(querySpaces); } return(cacheable ? ListUsingQueryCache(querySpaces) : ListIgnoreQueryCache()); } finally { After(); } } }
/// <summary> /// Return the query results of all the queries /// </summary> public IList List() { using (session.BeginProcess()) { bool cacheable = session.Factory.Settings.IsQueryCacheEnabled && isCacheable; combinedParameters = CreateCombinedQueryParameters(); if (log.IsDebugEnabled()) { log.Debug("Multi query with {0} queries.", queries.Count); for (int i = 0; i < queries.Count; i++) { log.Debug("Query #{0}: {1}", i, queries[i]); } } try { Before(); return(cacheable ? ListUsingQueryCache() : ListIgnoreQueryCache()); } finally { After(); } } }
protected virtual async Task <DateTime> UsePreparedStatementAsync(string timestampSelectString, ISessionImplementor session, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var tsSelect = new SqlString(timestampSelectString); DbCommand ps = null; DbDataReader rs = null; using (session.BeginProcess()) { try { ps = await(session.Batcher.PrepareCommandAsync(CommandType.Text, tsSelect, EmptyParams, cancellationToken)).ConfigureAwait(false); rs = await(session.Batcher.ExecuteReaderAsync(ps, cancellationToken)).ConfigureAwait(false); await(rs.ReadAsync(cancellationToken)).ConfigureAwait(false); var ts = rs.GetDateTime(0); log.DebugFormat("current timestamp retreived from db : {0} (ticks={1})", ts, ts.Ticks); return(ts); } catch (DbException sqle) { throw ADOExceptionHelper.Convert( session.Factory.SQLExceptionConverter, sqle, "could not select current db timestamp", tsSelect); } finally { if (ps != null) { try { session.Batcher.CloseCommand(ps, rs); } catch (DbException sqle) { log.Warn("unable to clean up prepared statement", sqle); } } } } }
protected virtual DateTime UsePreparedStatement(string timestampSelectString, ISessionImplementor session) { var tsSelect = new SqlString(timestampSelectString); DbCommand ps = null; DbDataReader rs = null; using (session.BeginProcess()) { try { ps = session.Batcher.PrepareCommand(CommandType.Text, tsSelect, EmptyParams); rs = session.Batcher.ExecuteReader(ps); rs.Read(); var ts = rs.GetDateTime(0); log.Debug("current timestamp retreived from db : {0} (ticks={1})", ts, ts.Ticks); return(ts); } catch (DbException sqle) { throw ADOExceptionHelper.Convert( session.Factory.SQLExceptionConverter, sqle, "could not select current db timestamp", tsSelect); } finally { if (ps != null) { try { session.Batcher.CloseCommand(ps, rs); } catch (DbException sqle) { log.Warn(sqle, "unable to clean up prepared statement"); } } } } }
public object PerformInsert(SqlCommandInfo insertSql, ISessionImplementor session, IBinder binder) { // NH-2145: Prevent connection releases between insert and select when we cannot perform // them as a single statement. Retrieving id most of the time relies on using the same connection. session.ConnectionManager.FlushBeginning(); try { try { // prepare and execute the insert var insert = session.Batcher.PrepareCommand(insertSql.CommandType, insertSql.Text, insertSql.ParameterTypes); try { binder.BindValues(insert); session.Batcher.ExecuteNonQuery(insert); } finally { session.Batcher.CloseCommand(insert, null); } } catch (DbException sqle) { throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "could not insert: " + persister.GetInfoString(), insertSql.Text); } var selectSql = SelectSQL; using (session.BeginProcess()) { try { //fetch the generated id in a separate query var idSelect = session.Batcher.PrepareCommand(CommandType.Text, selectSql, ParametersTypes); try { BindParameters(session, idSelect, binder); var rs = session.Batcher.ExecuteReader(idSelect); try { return(GetResult(session, rs, binder.Entity)); } finally { session.Batcher.CloseReader(rs); } } finally { session.Batcher.CloseCommand(idSelect, null); } } catch (DbException sqle) { throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "could not retrieve generated id after insert: " + persister.GetInfoString(), insertSql.Text); } } } finally { session.ConnectionManager.FlushEnding(); } }
public async Task <object> PerformInsertAsync(SqlCommandInfo insertSql, ISessionImplementor session, IBinder binder, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); // NH-2145: Prevent connection releases between insert and select when we cannot perform // them as a single statement. Retrieving id most of the time relies on using the same connection. session.ConnectionManager.FlushBeginning(); try { try { // prepare and execute the insert var insert = await(session.Batcher.PrepareCommandAsync(insertSql.CommandType, insertSql.Text, insertSql.ParameterTypes, cancellationToken)).ConfigureAwait(false); try { await(binder.BindValuesAsync(insert, cancellationToken)).ConfigureAwait(false); await(session.Batcher.ExecuteNonQueryAsync(insert, cancellationToken)).ConfigureAwait(false); } finally { session.Batcher.CloseCommand(insert, null); } } catch (DbException sqle) { throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "could not insert: " + persister.GetInfoString(), insertSql.Text); } var selectSql = SelectSQL; using (session.BeginProcess()) { try { //fetch the generated id in a separate query var idSelect = await(session.Batcher.PrepareCommandAsync(CommandType.Text, selectSql, ParametersTypes, cancellationToken)).ConfigureAwait(false); try { await(BindParametersAsync(session, idSelect, binder.Entity, cancellationToken)).ConfigureAwait(false); var rs = await(session.Batcher.ExecuteReaderAsync(idSelect, cancellationToken)).ConfigureAwait(false); try { return(await(GetResultAsync(session, rs, binder.Entity, cancellationToken)).ConfigureAwait(false)); } finally { session.Batcher.CloseReader(rs); } } finally { session.Batcher.CloseCommand(idSelect, null); } } catch (DbException sqle) { throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "could not retrieve generated id after insert: " + persister.GetInfoString(), insertSql.Text); } } } finally { session.ConnectionManager.FlushEnding(); } }