//-----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
//
//-----------------------------------------------------------------------
namespace Microsoft.Isam.Esent.Interop
{
using System.Globalization;
///
/// A class that encapsulates a JET_SESID in a disposable object.
///
public class Session : EsentResource
{
///
/// The underlying JET_SESID.
///
private JET_SESID sesid;
///
/// Initializes a new instance of the Session class. A new
/// JET_SESSION is allocated from the given instance.
///
/// The instance to start the session in.
public Session(JET_INSTANCE instance)
{
Api.JetBeginSession(instance, out this.sesid, null, null);
this.ResourceWasAllocated();
}
///
/// Gets the JET_SESID that this session contains.
///
public JET_SESID JetSesid
{
get
{
this.CheckObjectIsNotDisposed();
return this.sesid;
}
}
///
/// Implicit conversion operator from a Session to a JET_SESID. This
/// allows a Session to be used with APIs which expect a JET_SESID.
///
/// The session to convert.
/// The JET_SESID of the session.
public static implicit operator JET_SESID(Session session)
{
return session.JetSesid;
}
///
/// Returns a that represents the current .
///
///
/// A that represents the current .
///
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "Session (0x{0:x})", this.sesid.Value);
}
///
/// Terminate the session.
///
public void End()
{
this.CheckObjectIsNotDisposed();
this.ReleaseResource();
}
///
/// Free the underlying JET_SESID.
///
protected override void ReleaseResource()
{
if (!this.sesid.IsInvalid)
{
Api.JetEndSession(this.JetSesid, EndSessionGrbit.None);
}
this.sesid = JET_SESID.Nil;
this.ResourceWasReleased();
}
}
}