java - How many times the ServiceLocator does do lookup operation in this case -



java - How many times the ServiceLocator does do lookup operation in this case -

could please help me in understanding servicelocator class nowadays in our project

this our servicelocator class

public class servicelocator { private static servicelocator instance; private context initalcontext; private map cache; // homecoming singelton service locator public static servicelocator getinstance () throws servicelocatorexception { if (instance == null) instance = new servicelocator(); homecoming instance; } private servicelocator () { seek { this.initalcontext = new initialcontext(); this.cache = collections.synchronizedmap(new hashmap()); } grab (namingexception ex) { system.err.printf("error in ctx lookup"); } } public datasource getdatasource (string datasourcename) { datasource datasource = null; seek { if (this.cache.containskey(datasourcename)) datasource = (datasource)this.cache.get(datasourcename); else { context envcontext = (context)initalcontext .lookup("java:comp/env"); datasource = (datasource)envcontext .lookup(datasourcename); this.cache.put(datasourcename, datasource); } } grab (namingexception ex) { system.err.printf("error in ctx lookup"); } homecoming datasource; } }

i have few questions respect above code .

is necessary create singleton or static synchronized method ??

why datasourcename set under map cache ?? (what happens if there 10 concurrent users operation 10 times or 1 time )

is 1 user can utilize database @ 1 time??

for reference client using service locator

private static final logger logger = logger.getlogger(myobject.class); private connection con; private datasource datasource; public myobject() { this.datasource = servicelocator.getinstance().getdatasource("jdbc/mydatabase"); }

1: needed singleton: in short, yes if want share across other threads or locations. singleton static method. in case can argue ought synchronized, though people prefer implementing them using enum avoid needing synchronized.

the why: servicelocator class made singleton multiple locations can phone call methods on same object, without creating anew.

the reason using singletons either because constructing instance takes much resources don't want more once, or because want create sure utilize 1 instance across calls method.

in case, singleton because servicelocator caches named datasources can reused when called anywhere else in code.

however singleton post not threadsafe. if utilize in multithreaded environment (like in application server) there possibility 2 threads both seek singleton. first thread might different singleton cache sec one.

(second , subsequent phone call actual singleton instance).

worse, in opinion, accessor code internal cache not threadsafe either. worst case scenario here double looking of datasource , 1 of 2 results lookup gets dropped again. since author of singleton uses synchronizedmap, get/put synchronized. decision to set not. there still race status here.

2: why set in cache:

basically if phone call getdatasource("abc") several times in row, first time datasource looked up/loaded. other times code find datasource in cache, , homecoming directly.

3: question :) depends on configuration. relatively safe no, many users/threads can access database @ same time.

http://docs.oracle.com/javase/7/docs/api/javax/sql/datasource.html states basic datasource manages 1 connection. utilize of singleton means threads can access same datasource @ same time, if single-connection datasource used, cause trouble.

however mutual utilize of datasource pooleddatasource represents grouping of connections. in case accessing many locations give each individual thread it's own database connection, making safe. when thread closes connection, homecoming connection pool utilize else.

java

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -