Thomas Bandt schrieb:
Hallo,
kannst du das auch nochmal idiotensicher erklären? Trotz Klimaanlage bin
ich heute wohl nicht so ganz in der Lage mich in sowas reinzudenken ...
Hmm, war es nicht Idiotensicher :) Also mittlerweile wird es nicht mehr
Session Spezifisch, sondern Thread spezifisch gemacht. Klasse anbei (nur
ein wenig umbenannt).
Aufruf über
FooBarDatabase db = FooBarDatabase.getInstance();
try {
db.Open();
SqlCommand cmd = new SqlCommand("SP_DING",db.Connection);
[...Datenbank Magie..]
}
finaly
{
db.Close();
}
Natürlich sollte fehler entsprechend gecatcht werden.
Da ein Öffnungszähler mitgeschleppt wird ist eine Verschachtelung
möglich. Du könntest in Page_Load() die Datenbank öffnen, und und
Page_Unload() die Connection mit Dispose() sicher schließen. Und überall
wo Du nun einen Datenbank Zugriff brauchst nimmst Du ein ähnliches
Statement wie oben.
Ob dies dem Connection Pooling überlegen ist? Keine Ahnung, stellt aber
sicher wenigsten die gleiche Connection für den Aufruf der Seite
verwendet wird.
Einen kleinen Performance Vorteil wird man habe, da ich denke das das
Dictionary schneller die passende Connection aufspürt als das Connection
Pooling.
Achso, diese Klasse nutzt einige .NET 2.0 spezifische Dinge wie Generics
und den ConfigurationManager;
Grüße
Albert
-----
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
namespace Foo.Bar.Database {
public class FooBarDatabase : IDisposable
{
private static Dictionary<int, FooBarDatabase> _dbs = null;
private static object _dbsLock = new object();
private string _connectionString = null;
private SqlConnection _connection = null;
private int _refCount = 0;
private int _openCount = 0;
private int _threadID = 0;
private FooBarDatabase()
{
_connectionString =
ConfigurationManager.ConnectionStrings["fooBarConnectionString"].ConnectionString;
}
~FooBarDatabase()
{
Dispose();
}
public static FooBarDatabase getInstance()
{
if (_dbs==null) _dbs = new Dictionary<int,FooBarDatabase>();
int threadID =
System.Threading.Thread.CurrentThread.ManagedThreadId;
FooBarDatabase db = null;
lock (_dbsLock)
{
if (!_dbs.ContainsKey(threadID))
{
db = new FooBarDatabase();
db._threadID = threadID;
_dbs.Add(threadID, db);
}
else
{
db = _dbs[threadID];
}
}
if (db!=null) db._refCount++;
return db;
}
public void Dispose()
{
if (_connection != null) {
_openCount = 0;
Close();
lock (_dbsLock)
{
_dbs.Remove(_threadID);
_connection = null;
}
}
}
public int RefCount
{
get { return _refCount; }
}
public SqlConnection Connection
{
get {
if (_connection == null)
{
_connection = new SqlConnection(_connectionString);
}
return _connection;
}
}
public bool Open()
{
if (Connection.State != ConnectionState.Open)
{
Connection.Open();
}
if (Connection.State == ConnectionState.Open)
{
_openCount++;
return true;
}
else
{
return false;
}
}
public void Close()
{
_openCount--;
if (_openCount <= 0)
{
if (Connection.State != ConnectionState.Closed)
{
Connection.Close();
}
_openCount = 0;
}
}
}
}
_______________________________________________
Asp.net Mailingliste, Postings senden an:
[email protected]
An-/Abmeldung und Suchfunktion unter:
http://www.glengamoi.com/mailman/listinfo/asp.net