Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. single static instance in multi-thread env.

single static instance in multi-thread env.

Scheduled Pinned Locked Moved C#
helpjavatoolsquestionworkspace
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    buchstaben
    wrote on last edited by
    #1

    hi, I'm stuck at a multi-thred issue. there is a class HibernateDAO with utility methods. this class is never instanciated, since its methods are static. HibernateDAO has a reference to ISessionFactory, which should be singleton. but this ISessionFactory will be instanciated for each thread seperately (logging will produce the output multiple times). can someone give me a hint why this behaviour occurs even after wrapping the access with _synclock variable?public class HibernateDAO<T> { static ILog log = LogManager.GetLogger(typeof(HibernateDAO<T>)); static object _synclock = new object(); static ISessionFactory sessionFactory; /// <summary> /// Gets the single session factory instance. /// </summary> /// <value>The session factory.</value> public static ISessionFactory SessionFactory { get { lock (_synclock) { if (sessionFactory == null) { NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure(); cfg.AddAssembly(typeof(T).Assembly); // replace decrypted password string conStr = (string)cfg.Properties["hibernate.connection.connection_string"]; log.Info("Trying to establish connection. Connectionstring: " + conStr); //cfg.SetInterceptor(new LoggingInterceptor()); sessionFactory = cfg.BuildSessionFactory(); } } return sessionFactory; } }
    thanks in advance.

    B L 2 Replies Last reply
    0
    • B buchstaben

      hi, I'm stuck at a multi-thred issue. there is a class HibernateDAO with utility methods. this class is never instanciated, since its methods are static. HibernateDAO has a reference to ISessionFactory, which should be singleton. but this ISessionFactory will be instanciated for each thread seperately (logging will produce the output multiple times). can someone give me a hint why this behaviour occurs even after wrapping the access with _synclock variable?public class HibernateDAO<T> { static ILog log = LogManager.GetLogger(typeof(HibernateDAO<T>)); static object _synclock = new object(); static ISessionFactory sessionFactory; /// <summary> /// Gets the single session factory instance. /// </summary> /// <value>The session factory.</value> public static ISessionFactory SessionFactory { get { lock (_synclock) { if (sessionFactory == null) { NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure(); cfg.AddAssembly(typeof(T).Assembly); // replace decrypted password string conStr = (string)cfg.Properties["hibernate.connection.connection_string"]; log.Info("Trying to establish connection. Connectionstring: " + conStr); //cfg.SetInterceptor(new LoggingInterceptor()); sessionFactory = cfg.BuildSessionFactory(); } } return sessionFactory; } }
      thanks in advance.

      B Offline
      B Offline
      Bekjong
      wrote on last edited by
      #2

      My guess would be it's because you're using generics (not a threading issue at all). Try running this:

      static void Main(string[] args)
      {
      object o = HibernateDAO<object>.SessionFactory;
      object s = HibernateDAO<string>.SessionFactory;
      object q = HibernateDAO<object>.SessionFactory;
      Console.WriteLine(object.ReferenceEquals(o, s) ? "o==s" : "o!=s");
      Console.WriteLine(object.ReferenceEquals(o, q) ? "q==s" : "q!=s");
      Console.ReadKey();
      }

      This wil get you: o!=s q==s o and s are not the same because you've actually referenced two different classes entirely: One initialized with an object and one initialized with a string. o and q are in fact the same class of type HibernateDAO<object>, which is not the same as type HibernateDAO<string>.

      Standards are great! Everybody should have one!

      1 Reply Last reply
      0
      • B buchstaben

        hi, I'm stuck at a multi-thred issue. there is a class HibernateDAO with utility methods. this class is never instanciated, since its methods are static. HibernateDAO has a reference to ISessionFactory, which should be singleton. but this ISessionFactory will be instanciated for each thread seperately (logging will produce the output multiple times). can someone give me a hint why this behaviour occurs even after wrapping the access with _synclock variable?public class HibernateDAO<T> { static ILog log = LogManager.GetLogger(typeof(HibernateDAO<T>)); static object _synclock = new object(); static ISessionFactory sessionFactory; /// <summary> /// Gets the single session factory instance. /// </summary> /// <value>The session factory.</value> public static ISessionFactory SessionFactory { get { lock (_synclock) { if (sessionFactory == null) { NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure(); cfg.AddAssembly(typeof(T).Assembly); // replace decrypted password string conStr = (string)cfg.Properties["hibernate.connection.connection_string"]; log.Info("Trying to establish connection. Connectionstring: " + conStr); //cfg.SetInterceptor(new LoggingInterceptor()); sessionFactory = cfg.BuildSessionFactory(); } } return sessionFactory; } }
        thanks in advance.

        L Offline
        L Offline
        LongRange Shooter
        wrote on last edited by
        #3

        Your code here does not show HibernateDAO as static. Also it cannot be static as written due to the generics. You have to make the class a strong type and define it explicitely as static in order to get a static class. All you've done is define a static method inside an instance class. You need something like this:

        public static class Hibernate
        {
        ...

        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups