Questions tagged [singleton]

0

votes
0

answer
2

Views

web application in linux environment throws javax.ejb.ConcurrentAccessTimeoutException Unable to get read lock on singleton ejb method

I have deployed EJB web application in tomcat,linux machine. This application connects to AWS RDS through JNDI lookup that presents in @singleton ejb class. Every minute my application prints the DB content in console. In initial startup of application, DB connection is achieved once using @PostCons...
user2728146
1

votes
2

answer
51

Views

Singleton usage creates memory leak in kotlin

Trying to use Singleton class in another Singleton, but somehow it gives me memory leaks. How could i improve it? Here is my example singleton implementation class FirstSingletonClass(val context: Context) { companion object { @Volatile private var instance: FirstSingletonClass? = null fun getInst...
1

votes
2

answer
44

Views

Does public static functions of base class become member functions of derived class if base class is inherited publicly?

I am trying to run the following code but getting error. #include template class Base { public : static T& Get() { static T t; return t; } }; class Derived : public Base { private : Derived() {} // friend Base; //un-commenting this will make code work. }; int main(...
kapil
1

votes
2

answer
2.2k

Views

servlet context and singleton object

I have seen an example of Singleton class object where it has a map containing user-id and password retrieved from database. This will help not to hit DB everytime during user login. Now i am thinking, can't this be done through ServletContext attribute. Basically on both the cases we will have appl...
Jayy
1

votes
3

answer
309

Views

Android - Singleton Class only updating on second refresh

I have a singleton class called DataManager. I update some data in DataManager when I call onStop() in a class (activity) called userProfile. It takes the values from some text input boxes and updates the User Object in DataManger. I navigate through my app with a BottomNavigationView - so when I pr...
Taylor Barton
1

votes
2

answer
48

Views

Is there a large performance hit when creating a class vs sticking with the singletonian nature of required modules?

I'm architecting the model layer on an MVC structured project. I'm looking at 2 solutions - listed below - and the performance hit from instantiating a new class every request. (if anyone else has another pattern recommendation, I'm open to it) Singleton const MySQLConnection = require('../configs/m...
lewicki
1

votes
1

answer
207

Views

Share ARSCNView on multiple ViewControllers

I want to build an app where users can swipe between different AR views filled with different content, but in the same AR session - so they don't need to search for plane/feature every time. I've tried to pass ARSCNView from one ViewController to another via a singleton. But it gives me error: Term...
Adam Roszyk
1

votes
0

answer
117

Views

Should I go tightly coupled, with singleton, or loosely coupled, without a singleton? Azure Services, Xamarin Forms

I am making a Xamarin Forms Cross platform app. I have been trying to figure out how to go about getting my app to integrate with the Azure Backend the best way. I have ran into a problem. It has been drilled into my head to try to build programs as loosely coupled as possible. But then I have been...
John Butler
1

votes
1

answer
26

Views

Install4j: how to check a RemoteCallable running unelevated

My installer is storing some information in a singleton class during the installation process. Now, I have noticed that in elevated action, the singleton class does not have the same instance. So far, I have not found any workaround/solution so that they share the same instance. So, I have decided t...
ShaDooW
1

votes
0

answer
70

Views

In what situation we are forced to have only one instance in object oriented?

As singleton design pattern is used to ensure only one instance of a class is created, I wander what problem would happen if we don't do so. For instance, look at this example which is written in Swift. let defaultFileManager = FileManager.default Singleton patten is used here to ensure that only on...
Fattaneh Talebi
1

votes
0

answer
25

Views

Objects data removed when back pressed.singleton object having data permanently

I'm having a singleton class A with some variables like name,age whose object holding values all over app. But inside singleton class another normal class(nested) B defined. which is having address(for example). In another fragment/activity A class object holding value but B class value holding at f...
Ragavendra M
1

votes
1

answer
185

Views

Typescript : force classes to implement singleton pattern

Is there any way in forcing a class to implement singleton pattern in typescript. If so how ca i achieve this ? So this is my interface and I would like that every class that implements it to be forced in implementing singleton export interface IObservable { } // marker export interface ICorporati...
MCR
1

votes
0

answer
67

Views

Run a periodic task in background independent from viewcontrollers

I'm not sure exactly how to correctly construct my question. It is actually more of a 'what is the advised practice' instead of an 'how to' type of question. So I'll try to explain my main goal first. I've started writing a small app which allows users to fill out forms and send them to a server....
monroo
1

votes
1

answer
137

Views

Is it better to have a single JAXBContext or separate contexts for each class?

I know JAXBContext should be created only once as it's pretty expensive, but I'm curious if it's preferable to create one context for all the required classes, JAXBContext newInstance(Class... classesToBeBound) Or keep all the contexts in a map and create them if they haven't been created yet? Map...
jjjjj
1

votes
2

answer
101

Views

Is this a valid Singleton class

If I return a reference to my instance of MySingleton from GetInstance() instead of the pointer in my code below, it will be valid, but is using the pointer in this way also valid? I assume my pointer will only get initialized once because it is static, but not not sure. I know the correct way by...
Engineer999
1

votes
0

answer
250

Views

singleton implementation via macro or template in C++

After referred Qt Singleton implementation. I got two solutions: The first, using template: template class Singleton { friend T; public: static T& instance(); private: Singleton() = default; ~Singleton() = default; Singleton( const Singleton& ) = delete; Singleton& operator=( const Singleton& ) = d...
Jiu
1

votes
2

answer
61

Views

Alternative to static overriding in java

I've been struggling with it for a while now. I want to define a finite set of types, some of which would be subtypes to others, where each type is defined by values of its properties. The types would then be used to access their properties and also by a factory method to create instances of them. I...
svatcher
1

votes
1

answer
254

Views

Using a List in a Singleton as simple in-memory cache/db (Java)

Imagine a simple java application as TCP-client and needing to hold a list of active/online users. (Delete/Update/Add User when a certain message is received) My first quick idea was to just implement a synchronized singleton with an instance of a CopyOnWriteArrayList (or similar) and methods for CR...
user2874177
1

votes
1

answer
52

Views

Good use case for singleton

In my app, I have a like feature where is are displayed users which liked your profile. Because the user receive push notification when another user like him, I decided to create a singleton ENFeedLikeManager to store my likes array and add object when a notification is coming: ENFeedLikeManager.sw...
Florian_L
1

votes
0

answer
44

Views

Map(value set using spring beans) is empty when I try to access using Rest URL

I am new to Spring Beans. I am trying to set entry map using beans.xml file and accessing that value using GET REST request. beans.xml ` In Main class, BookService bookService = (BookService) context.getBean('bookservice2'); bookService.getMap().toString(); // here it is working fine.` I guess when...
AwesomeBlossom
1

votes
1

answer
542

Views

Flutter Dart Singleton Data 'Lost'

I want to use the following class to store account data to make it accessible to every part of the app. class AccountData { static AccountData _instance = new AccountData._internal(); static GoogleSignInAccount _googleData; factory AccountData() { return _instance; } AccountData._internal(); static...
tim s
1

votes
0

answer
136

Views

Singleton TypeError: object() takes no parameters

I encountered this problem when I used the __new__ method as a singleton: In [1]: class Singleton(object): ...: __instances = None ...: ...: def __new__(cls, *args, **kwargs): ...: if not cls.__instances: ...: cls.__instances = super(Singleton, cls).__new__(cls, *args, **...
kevin wong
1

votes
0

answer
76

Views

Singleton/Synchronization in Clustered environment

How to achieve singleton instance in clustered environment or how can we have a synchronization of a method in clustered environment using in-memory cache like Redis.
1

votes
1

answer
158

Views

Is final necessary for a singleton class in Swift?

To create a singleton class, I wrote something like this: class SingletonEx{ var name = '' private init(){} static let sharedInstance = SingletonEx() func instanceMethod(){ } static func classMethod(){ } } Some tutorials say final is necessary while others just ignore final keyword. After I tried...
Chao Peng
1

votes
0

answer
23

Views

Use Serializable or modified Singleton?

I have an Application with a List of Objects on the main screen. When clicking on one of them, I'm transferring the user to the next Fragment. Then I have more 3 Fragments which are setting(manipulating) the object fields. My question is, is it right instead of implementing the Serializable interfac...
Igor Fridman
1

votes
1

answer
210

Views

Kotlin singleton in a subclass

I'm writing a framework in which I have difficulties to write a generic singleton class which need to be specialized by the user. I would like the use being able to write something like: class CustomClass protected constructor() : BasicClass() with the framework being able to write: BasicClass.insta...
AnthonyCFE
1

votes
0

answer
51

Views

IOS-Swift-Is the following singleton implementation really thread-safe in swift?

I normally wrote the following implementation of singleton class, I am wondering if this is thread-safe: final class SomeSingleton{ static let sharedInstance = SomeSingleton() private init(){} } I search a lot of blogs. Some say it guarantees thread-safety for the following possible reasons: 'by...
Chao Peng
1

votes
1

answer
45

Views

Does exporting a function creates a singleton of the function?

I am trying to implement a abstraction on top of my API. I need to have a single instance of the axios client so I can intercept requests at a later stage. With the code below I try to reuse the same instance. Does the code below guarantee that there is only one axios client when I am importing the...
tomwassing
1

votes
1

answer
171

Views

Single Instance of HttpClient with UseDefaultCredentials

I have been trying to update my API call using the suggestion from here to only have 1 instance of HttpClient. https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/# This works fine and I have my do not use up all of the ports, however when I try to create a HttpClientHandler to pass alon...
Luke Konecki
1

votes
1

answer
75

Views

Zend View behaviour in a Swoole http server

we are facing a strange behaviour with a Zend Expressive application using Zend View and running in a swoole http server. This behaviour is related with the Singleton pattern. We have configured Zend Expressive to run in a swoole http server - https://docs.zendframework.com/zend-expressive-swoole/ -...
Javier Gálvez
1

votes
0

answer
76

Views

Singleton class not working as expected C++

I am working on a project that involves creating a singleton class.. So I have created a shared library for the singleton class and created two binaries to use this class. When the first exe calls the class's static function 'getInstance()' a new instance of Singleton is created and this is expected...
Akshay Vernekar
4

votes
3

answer
9.5k

Views

Using a singleton across DLL boundary [duplicate]

This question already has an answer here: C++ a singleton class with dll 3 answers I have a legacy project which has a singleton class like this: class Singleton { public: static Singleton& Instance() { static Singleton inst; return inst; } void foo(); }; The project uses a DLL which needs to use...
Tamás Szelei
1

votes
0

answer
37

Views

How junit's BeforeClass annotation behaves in a top level object in scala?

I have a beginner's question: I have a spark based scala project that uses junit. This is the important part of the project: object ResearchCrawlServiceTestSuite { implicit var ss: SparkSession = _ var fs: FileSystem = _ @BeforeClass def init(): Unit = { ss = SparkSession.builder().master('local').g...
David Rimoch
1

votes
0

answer
24

Views

Injecting Singleton Into Guice AOP Method Interceptor

I have a method interceptor that I inject a class X that is annotated with @Singleton. I noticed that unless I additionally bind that class X as an EagerSingleton Guice creates two instances of that Singleton class. One that is used by my method interceptor and one that is used by all other classes...
patel183
1

votes
1

answer
61

Views

What the correct way when use Double-Checked Locking with memory barrier in c++?

I just read the excellent blog C++ and the Perils of Double-Checked Locking And I don't understand why we have to use the first memory barrier in Example 12 (as below): Singleton* Singleton::instance () { Singleton* tmp = pInstance; ... // insert memory barrier if (tmp == 0) { Lock lock; tmp = pInst...
ricky
1

votes
0

answer
185

Views

Should you unsubscribe from an event in an Angular Service?

We have several Angular services that subscribe to an event in the constructor. We are trying to determine whether it's necessary to unsubscribe from the event in the ngOnDestroy() method. Here's an example of the code: import { Injectable, OnDestroy } from '@angular/core'; @Injectable() export cl...
JimP
1

votes
1

answer
31

Views

Scope import for an instance only

Good evening to everyone. I'm not sure how can I explain my issue. I will show it to you by showing examples of the code and expected results. I could not use code from the real issue because the code is under license. I am very sorry for that and I will be glad of someone can help me solve my issue...
siOnzee
1

votes
0

answer
276

Views

React - Provide Global Singleton From Composition Root

To start off, I have been working with React now for three months and the application I am building is testable, performant, etc... Nothing wrong. My experience pre-React is from the Angular world and what is considered a best practice there is not normally in react and vice-a-versa... I don't think...
Ryan McCormick
1

votes
2

answer
74

Views

NoClassDefFoundError while calling Java singleton class in kotlin Class

how I'll be able to get data from Singleton class of java in kotlin class.? since my project is an old project and whole app is in java now I'm implementing its new module in kotlin where I'm using Java's Singleton (CurrentUser.java) in kotlin's Service Class(UpdateWidgetService.kt).while I'm usin...
Atif AbbAsi
1

votes
0

answer
41

Views

Scala Type Equivalence Checking on Singleton Types

I have a function to witness type equivalence: def typeEq[A, B](a: A, b: B)(implicit ev: A =:= B) = println('happy') typeEq: [A, B](a: A, b: B)(implicit ev: A =:= B)Unit It fails to check the following two different singleton types: class X val a = new X; val b = new X val aa:a.type = a; val bb:b.ty...
Michael Wang

View additional questions