How to constrain a table to contain a single row?

You make sure one of the columns can only contain one value, and then make that the primary key (or apply a uniqueness constraint). CREATE TABLE T1( Lock char(1) not null, /* Other columns */, constraint PK_T1 PRIMARY KEY (Lock), constraint CK_T1_Locked CHECK (Lock=’X’) ) I have a number of these tables in various databases, … Read more

How do you implement the Singleton design pattern?

In 2008 I provided a C++98 implementation of the Singleton design pattern that is lazy-evaluated, guaranteed-destruction, not-technically-thread-safe: Can any one provide me a sample of Singleton in c++? Here is an updated C++11 implementation of the Singleton design pattern that is lazy-evaluated, correctly-destroyed, and thread-safe. class S { public: static S& getInstance() { static S … Read more

How do I implement convenient logging without a Singleton?

First: the use of std::unique_ptr is unnecessary: void Log::LogMsg(std::string const& s) { static Log L; L.log(s); } Produces exactly the same lazy initialization and cleanup semantics without introducing all the syntax noise (and redundant test). Now that is out of the way… Your class is extremely simple. You might want to build a slightly more … Read more

ASP .NET Singleton

Static members have a scope of the current worker process only, so it has nothing to do with users, because other requests aren’t necessarily handled by the same worker process. In order to share data with a specific user and across requests, use HttpContext.Current.Session. In order to share data within a specific request, use HttpContext.Current.Items. … Read more

How to use scala.None from Java code [duplicate]

The scala.None$.MODULE$ thing doesn’t always typecheck, for example this doesn’t compile: scala.Option<String> x = scala.None$.MODULE$; because javac doesn’t know about Scala’s declaration-site variance, so you get: J.java:3: incompatible types found : scala.None$ required: scala.Option<java.lang.String> scala.Option<String> x = scala.None$.MODULE$ ; This does compile, though: scala.Option<String> x = scala.Option.apply(null); so that’s a different way to get a … Read more

“Singleton” factories, ok or bad?

It really depends on what you’re doing and the scope of your application. If it’s just a fairly small app and it’s never going to grow beyond this, then your current approach may well be fine. There is no universal “best” practice for these things. While I wouldn’t recommend using singletons for anything other than … Read more