我有一个相当理论的问题ThreadLocal<T>
,由于周围世界偶尔发生的事件,例如连接丢失,我的 T 可能会变得无法使用。关于如何重置 ThreadLocal 值的任何想法,相对于对象变得不可用的线程,而不重置整个ThreadLocal<T>
对象?
顺便一提
我的上下文是 ASP.NET MVC,我做了类似的事情
ThreadLocal<DatabaseConnection>
(在我的情况下有充分的理由这样做)因此,让用户查看“砰!您的程序出错,因为编写此程序的开发人员是脑死亡僵尸”之类的异常的解决方案不是一种选择......
祝您玩得开心,并提前感谢您。
编辑:想象一个 Web 服务器,它有一个由 8 个线程组成的池,每分钟处理 512 个客户端请求,我使用上述解决方案将打开的连接数量限制为每分钟 8 个连接,而不是在同一时期内创建 512 个时间...
编辑:这是与我的问题相关的来源,我问自己一个问题“如果在某个特定线程中与数据库的连接丢失怎么办”我将如何在 ThreadLocal 中重新创建和重新设置它:
公共类 EloqueraThreadLocalInstanceFactory
public class EloqueraThreadLocalInstanceFactory
{
Property<DB> database;
private DB CreateInstance()
{
var db = new DB(ConnectionString);
try
{
db.OpenDatabase(DatabaseName);
}
catch (FileNotFoundException)
{
db.CreateDatabase(DatabaseName);
db.OpenDatabase(DatabaseName);
}
return db;
}
public EloqueraThreadLocalInstanceFactory(String connectionString, String databaseName, params Action<DB>[] initializers)
{
DatabaseName = databaseName;
ConnectionString = connectionString;
database = new ThreadLocalPropertyValue<DB>(CreateInstance, initializers);
}
public String DatabaseName { get; private set; }
public String ConnectionString { get; private set; }
public DB GetInstance()
{
return database;
}
}
公共类属性 { ... }
public class Property<T>
{
private PropertyValue<T> propertyValue;
protected virtual void OnAfterSet()
{
if (AfterSet != null)
{
AfterSet(this, null);
}
}
protected virtual void OnBeforeGet(EventArgs<T> eventArgs)
{
if (BeforeGet != null)
{
BeforeGet(this, eventArgs);
}
}
protected virtual void OnBeforeSet(CancellableEventArgs<PropertyValue<T>> e)
{
if (BeforeSet != null)
{
BeforeSet(this, e);
}
}
public Property()
{
}
public Property(PropertyValue<T> propertyValue)
{
this.propertyValue = propertyValue;
}
public void Reset()
{
propertyValue = null;
}
// IsSet is required to be implemented via method to prevent it from being serialized
public Boolean IsSet()
{
return propertyValue != null;
}
// Value property is required for serialization purposes
public T Value
{
get { return Get(); }
set { Set(new StaticPropertyValue<T>(value)); }
}
protected void SetRaw(PropertyValue<T> propertyValue)
{
this.propertyValue = propertyValue;
}
protected virtual T Get()
{
var eventArgs = new EventArgs<T>(propertyValue.GetPropertyValueInternal());
OnBeforeGet(eventArgs);
return eventArgs.Value;
}
protected virtual void Set(PropertyValue<T> propertyValue)
{
var eventArgs = new CancellableEventArgs<PropertyValue<T>>(propertyValue);
OnBeforeSet(eventArgs);
this.propertyValue = propertyValue;
if (!eventArgs.Cancel)
{
SetRaw(propertyValue);
OnAfterSet();
}
}
public event EventHandler AfterSet;
public event EventHandler<EventArgs<T>> BeforeGet;
public event EventHandler<CancellableEventArgs<PropertyValue<T>>> BeforeSet;
public static implicit operator T(Property<T> property)
{
return property == null ? default(T) : property.Get();
}
public static implicit operator Property<T>(T value)
{
return new Property<T>(new StaticPropertyValue<T>(value));
}
public static implicit operator Property<T>(Func<T> value)
{
return new Property<T>(new LazyPropertyValue<T>(value));
}
public static implicit operator Property<T>(PropertyValue<T> value)
{
return new Property<T>(value);
}
public static implicit operator PropertyValue<T>(Property<T> property)
{
return property.propertyValue;
}
/***************************************************************************************************/
public static Property<T> operator <=(Property<T> property, Func<T> propertyValue)
{
if (property != null)
{
property.Set(new LazyPropertyValue<T>(propertyValue));
}
else
{
return new Property<T>(new LazyPropertyValue<T>(propertyValue));
}
return property;
}
public static Property<T> operator >=(Property<T> property, Func<T> propertyValue)
{
property.Set(new LazyPropertyValue<T>(propertyValue));
return property;
}
public static Property<T> operator <=(Property<T> property, PropertyValue<T> propertyValue)
{
if (property != null)
{
property.Set(propertyValue);
}
else
{
return new Property<T>(propertyValue);
}
return property;
}
public static Property<T> operator >=(Property<T> property, PropertyValue<T> propertyValue)
{
property.Set(propertyValue);
return property;
}
public static Property<T> operator <=(Property<T> property, LazyPropertyValue<T> propertyValue)
{
if (property != null)
{
property.Set(propertyValue);
}
else
{
return new Property<T>(propertyValue);
}
return property;
}
public static Property<T> operator >=(Property<T> property, LazyPropertyValue<T> propertyValue)
{
property.Set(propertyValue);
return property;
}
public static Property<T> operator <=(Property<T> property, StaticPropertyValue<T> propertyValue)
{
if (property != null)
{
property.Set(propertyValue);
}
else
{
return new Property<T>(propertyValue);
}
return property;
}
public static Property<T> operator >=(Property<T> property, StaticPropertyValue<T> propertyValue)
{
property.Set(propertyValue);
return property;
}
/***************************************************************************************************/
public static Property<T> operator <(Property<T> property, Func<T> propertyValue)
{
if (property != null)
{
property.SetRaw(new LazyPropertyValue<T>(propertyValue));
}
else
{
return new Property<T>(new LazyPropertyValue<T>(propertyValue));
}
return property;
}
public static Property<T> operator >(Property<T> property, Func<T> propertyValue)
{
property.SetRaw(new LazyPropertyValue<T>(propertyValue));
return property;
}
public static Property<T> operator <(Property<T> property, PropertyValue<T> propertyValue)
{
if (property != null)
{
property.SetRaw(propertyValue);
}
else
{
return new Property<T>(propertyValue);
}
return property;
}
public static Property<T> operator >(Property<T> property, PropertyValue<T> propertyValue)
{
property.SetRaw(propertyValue);
return property;
}
public static Property<T> operator <(Property<T> property, LazyPropertyValue<T> propertyValue)
{
if (property != null)
{
property.SetRaw(propertyValue);
}
else
{
return new Property<T>(propertyValue);
}
return property;
}
public static Property<T> operator >(Property<T> property, LazyPropertyValue<T> propertyValue)
{
property.SetRaw(propertyValue);
return property;
}
public static Property<T> operator <(Property<T> property, StaticPropertyValue<T> propertyValue)
{
if (property != null)
{
property.SetRaw(propertyValue);
}
else
{
return new Property<T>(propertyValue);
}
return property;
}
public static Property<T> operator >(Property<T> property, StaticPropertyValue<T> propertyValue)
{
property.SetRaw(propertyValue);
return property;
}
}
public abstract class PropertyValue<T>
{
protected abstract T GetPropertyValue();
internal T GetPropertyValueInternal()
{
return GetPropertyValue();
}
}
public class InitializablePropertyValue<T> : PropertyValue<T>
{
private Func<T> func;
private Action<T>[] initializers;
public InitializablePropertyValue(Func<T> func, params Action<T>[] initializers)
{
if (func == null) throw new ArgumentNullException("func");
if (initializers == null) throw new ArgumentNullException("initializers");
this.func = func;
this.initializers = initializers;
}
protected override T GetPropertyValue()
{
var result = func();
foreach (var initializer in initializers)
{
initializer(result);
}
return result;
}
}
public class StaticPropertyValue<T> : PropertyValue<T>
{
private T value;
public StaticPropertyValue(T value)
{
this.value = value;
}
protected override T GetPropertyValue()
{
return value;
}
public static implicit operator StaticPropertyValue<T>(T value)
{
return new StaticPropertyValue<T>(value);
}
}
public class LazyPropertyValue<T> : InitializablePropertyValue<T>
{
private Lazy<T> lazy;
public LazyPropertyValue(Func<T> valueFactory)
: base(valueFactory)
{
this.lazy = new Lazy<T>(base.GetPropertyValue, false);
}
public LazyPropertyValue(Func<T> valueFactory, Boolean isThreadSafe)
: base(valueFactory)
{
this.lazy = new Lazy<T>(base.GetPropertyValue, isThreadSafe);
}
public LazyPropertyValue(Func<T> valueFactory, LazyThreadSafetyMode mode)
: base(valueFactory)
{
this.lazy = new Lazy<T>(base.GetPropertyValue, mode);
}
public LazyPropertyValue(Func<T> valueFactory, params Action<T>[] initializers)
: base(valueFactory, initializers)
{
this.lazy = new Lazy<T>(base.GetPropertyValue, false);
}
public LazyPropertyValue(Func<T> valueFactory, Boolean isThreadSafe, params Action<T>[] initializers)
: base(valueFactory, initializers)
{
this.lazy = new Lazy<T>(base.GetPropertyValue, isThreadSafe);
}
public LazyPropertyValue(Func<T> valueFactory, LazyThreadSafetyMode mode, params Action<T>[] initializers)
: base(valueFactory, initializers)
{
this.lazy = new Lazy<T>(base.GetPropertyValue, mode);
}
public Boolean IsInitialized
{
get { return lazy.IsValueCreated; }
}
protected override T GetPropertyValue()
{
return lazy.Value;
}
public static implicit operator LazyPropertyValue<T>(Func<T> value)
{
return new LazyPropertyValue<T>(value);
}
}
public class ThreadLocalPropertyValue<T> : InitializablePropertyValue<T>
{
ThreadLocal<T> threadLocal;
public ThreadLocalPropertyValue(Func<T> func, params Action<T>[] initializers)
: base(func, initializers)
{
this.threadLocal = new ThreadLocal<T>(base.GetPropertyValue);
}
protected override T GetPropertyValue()
{
return threadLocal.Value;
}
}
public class DynamicPropertyValue<T> : PropertyValue<T>
{
Func<T> func;
public DynamicPropertyValue(Func<T> func)
{
this.func = func;
}
protected override T GetPropertyValue()
{
return func();
}
}