Android: Cannot perform this operation because the connection pool has been closed

Remove db.close(); If you try another operation after closing the database, it will give you that exception. The documentation says: Releases a reference to the object, closing the object… Also, check out Android SQLite closed exception about a comment from an Android Framework engineer which states that it is not necessary to close the database … Read more

Thread safety in Singleton

Josh Bloch recommends using a single-element enum type to implement singletons (see Effective Java 2nd Edition, Item 3: Enforce the singleton property with a private constructor or an enum type). Some people think this is a hack, since it doesn’t clearly convey intent, but it does work. The following example is taken straight from the … Read more

Javascript: best Singleton pattern [duplicate]

(1) UPDATE 2019: ES7 Version class Singleton { static instance; constructor() { if (instance) { return instance; } this.instance = this; } foo() { // … } } console.log(new Singleton() === new Singleton()); (2) ES6 Version class Singleton { constructor() { const instance = this.constructor.instance; if (instance) { return instance; } this.constructor.instance = this; } … Read more

Singleton in Swift

The standard singleton pattern is: final class Manager { static let shared = Manager() private init() { … } func foo() { … } } And you’d use it like so: Manager.shared.foo() Credit to appzYourLife for pointing out that one should declare it final to make sure it’s not accidentally subclassed as well as the … Read more

When should EntityManagerFactory instance be created/opened?

EntityManagerFactory instances are heavyweight objects. Each factory might maintain a metadata cache, object state cache, EntityManager pool, connection pool, and more. If your application no longer needs an EntityManagerFactory, you should close it to free these resources. When an EntityManagerFactory closes, all EntityManagers from that factory, and by extension all entities managed by those EntityManagers, … Read more

Dependency Injection vs Service Location

Because the class is now being injected with an IoC container, then why not use it to resolve all other dependencies too? Using the service locator pattern completely defeats one of the main points of dependency injection. The point of dependency injection is to make dependencies explicit. Once you hide those dependencies by not making … Read more

Get application context from non activity singleton class

Update: 06-Mar-18 Use MyApplication instance instead of Context instance. Application instance is a singleton context instance itself. public class MyApplication extends Application { private static MyApplication mContext; @Override public void onCreate() { super.onCreate(); mContext = this; } public static MyApplication getContext() { return mContext; } } Previous Answer You can get the the application context … Read more

tech