#region Apache Notice
/*****************************************************************************
* $Header: $
* $Revision: 515691 $
* $Date$
*
* iBATIS.NET Data Mapper
* Copyright (C) 2008/2005 - The Apache Software Foundation
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
********************************************************************************/
#endregion
using System;
using System.Data;
using System.Diagnostics;
using Apache.Ibatis.Common.Contracts;
using Apache.Ibatis.Common.Logging;
using Apache.Ibatis.DataMapper.Exceptions;
using Apache.Ibatis.DataMapper.Session.Transaction;
namespace Apache.Ibatis.DataMapper.Session
{
///
/// Default implementation of the contract.
///
[DebuggerDisplay("DataMapperSession: {Connection}")]
public class DataMapperSession : ISession
{
private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
[NonSerialized]
private readonly ISessionFactory sessionFactory = null;
[NonSerialized]
private readonly ITransactionManager transactionManager = null;
[NonSerialized]
private IDbConnection connection = null;
[NonSerialized]
private readonly bool autoClose = false;
[NonSerialized]
private ITransaction transaction = null;
///
/// Initializes a new instance of the class.
///
/// The connection.
/// The factory.
/// The transaction manager.
/// if set to true [auto close].
public DataMapperSession(
IDbConnection connection,
ISessionFactory factory,
ITransactionManager transactionManager,
bool autoClose)
{
Contract.Require.That(connection, Is.Not.Null).When("retrieving argument connection in DataMapperSession constructor");
Contract.Require.That(factory, Is.Not.Null).When("retrieving argument factory in DataMapperSession constructor");
this.connection = connection;
sessionFactory = factory;
this.transactionManager = transactionManager;
this.autoClose = autoClose;
}
#region ISession Members
///
/// Get the that created this instance.
///
///
public ISessionFactory SessionFactory
{
get { return sessionFactory; }
}
///
/// Gets the ADO.NET connection.
///
///
///
/// Applications are responsible for calling commit/rollback upon the connection before
/// closing the ISession.
///
public IDbConnection Connection
{
get { return connection; }
}
///
/// Opens the connection.
///
/// The connection provided by the DataMapper
public IDbConnection OpenConnection()
{
if (connection.State != ConnectionState.Open)
{
try
{
connection.Open();
if (logger.IsDebugEnabled)
{
logger.Debug(string.Format("Open Connection \"{0}\" to \"{1}\".", connection.GetHashCode(), sessionFactory.DataSource.DbProvider.Description));
}
}
catch (Exception ex)
{
throw new DataMapperException(string.Format("Unable to open connection to \"{0}\".", sessionFactory.DataSource.DbProvider.Description), ex);
}
}
return connection;
}
///
/// End the ISession by disconnecting from the ADO.NET connection and cleaning up.
///
///
/// The connection provided by the DataMapper or
///
public IDbConnection Close()
{
logger.Debug("Closing session");
try
{
sessionFactory.Dispose();
if (connection == null)
{
return null;
}
else
{
// get a new reference to the the Session's connection before
// closing it - and set the existing to Session's connection to
// null but don't close it yet
IDbConnection connectionRef = connection;
connection = null;
// if Session is supposed to auto-close the connection then
// the Sesssion is managing the IDbConnection.
if (autoClose)
{
if (logger.IsDebugEnabled)
{
logger.Debug(string.Format("Close Connection \"{0}\" to \"{1}\".", connectionRef.GetHashCode(), sessionFactory.DataSource.DbProvider.Description));
}
connectionRef.Close();
connectionRef.Dispose();
return null;
}
else
{
return connectionRef;
}
}
}
finally
{}
}
///
/// Begin a unit of work and return the associated ITransaction object.
///
/// A transaction instance
public ITransaction BeginTransaction()
{
transaction = transactionManager.BeginTransaction(this);
//isCurrentTransaction = true;
return transaction;
}
///
/// Begin a transaction with the specified isolationLevel
///
/// Isolation level for the new transaction
///
/// A transaction instance having the specified isolation level
///
public ITransaction BeginTransaction(IsolationLevel isolationLevel)
{
transaction = transactionManager.BeginTransaction(this, isolationLevel);
//isCurrentTransaction = true;
return transaction;
}
///
/// Get the current associated ITransaction object.
///
///
public ITransaction Transaction
{
get { return transaction; }
}
#endregion
#region IDisposable Members
///
/// A flag to indicate if Disose() has been called.
///
private bool isAlreadyDisposed = false;
///
/// Releases unmanaged resources and performs other cleanup operations before the
/// is reclaimed by garbage collection.
///
~DataMapperSession()
{
Dispose( false );
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
logger.Debug( "running ISession.Dispose()" );
Dispose( true );
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
public void Dispose(bool isDisposing)
{
if( isAlreadyDisposed )
{
// don't dispose of multiple times.
return;
}
// free managed resources that are being managed by the session if we
// know this call came through Dispose()
if( isDisposing )
{
if( transaction != null )
{
transaction.Dispose();
}
Close();
}
isAlreadyDisposed = true;
// nothing for Finalizer to do - so tell the GC to ignore it
GC.SuppressFinalize( this );
}
#endregion
}
}