• Post Reply Bookmark Topic Watch Topic
  • New Topic

Suggestion for Architecture  RSS feed

 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am working on a project using servlets and hibernate which will be deployed on jboss 6 server.
For a request to my application I want to connect to DB only one time and load couple of tables data into java Bean/Map. I do not want to connect to DB for every internal request of one request.

Any help..?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hrithik khurana wrote:I am working on a project using servlets and hibernate which will be deployed on jboss 6 server.
For a request to my application I want to connect to DB only one time and load couple of tables data into java Bean/Map. I do not want to connect to DB for every internal request of one request.
Any help..?

Unfortunately, without more specific information it's very difficult to advise; but it sounds to me like you want to cache certain items. That's generally fine if you're only reading those things, but if they can also be updated, then you may simply be adding a layer that you don't need.

However, as I said, for more advice, you need to TellTheDetails.

Winston
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are building your own data access layer, you could implement the data access layer to encapsulate the map. It can from database and stores in the map on initialization. COnsecutive calls can query the map. this way you will hide the implementation of map inside the data access layer
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aside from what Winston already said, it sounds to me like you're doing some premature optimization. That, as Donald Knuth said, is the root of all evil. You might want to come up with a "good enough" solution first, then worry about limiting the number of database connections after a profiler has shown you that it's actually a problem that is impacting some quality attribute requirement related to things like responsiveness or resource utilization.
 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have started developing the project recently and here's what I have done till date

Hibernate Utility class



For my Data Access Layers, I'm using the database conn as below



I have a couple of tables which are used in many methods like above, how do I load them into a Map/Bean at the very first time and use them for all the consecutive calls.

Please help !!
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hrithik khurana wrote:I have a couple of tables which are used in many methods like above, how do I load them into a Map/Bean at the very first time and use them for all the consecutive calls.


Don't duplicate, abstract details away and encapsulate. Put all the detailed implementation logic that accesses the data into one class/method. If, for some very good reason, you decide to cache the data, then you can implement caching in that one class. The rest of your code just uses this class to get specific data without having to worry about whether or not there's caching, multiple db connections, or whatever other non-functional concern.

I notice that you're using static methods where it doesn't seem like you should be. This is not a good practice if you want to produce object-oriented designs and code.
 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys

On the first servlet's (load-on-startup =1) init method i have queried for the data and now I have a map with data from DB, How do i access this in the further servlets..?

http://google.com/Washington

@ http://google.com/ ==> my first servlet is invoked and map is populated with data , now how do I access that map in the servelt - Washington or in the further data layer of Washington servlet.

Also a periodic refresh of data should happen, I was planning to go with the 'Timer' .

Any suggestions..?

Thanks.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hrithik khurana wrote:Also a period refresh of data should happen, I was planning to go with the 'Timer' .

And here's where I was talking about "layers you may not need"...

Caches are notoriously finicky things, and have an alarming tendency to mushroom in size and complexity as people add all sorts of "nice to have" features to them; to the point where they often consume more time and effort than the thing they are actually caching for.

Did you read Junilu's first post? Can you actually prove that this cache is going to save you significant amounts of time? Have you done any benchmarks or profiling? I strongly urge you to do all of these things before you spend one more second on this.

Caches work best when they're:
(a) simple.
(b) work on static data.
As soon as you get into things like refreshing (and I suspect the next thing will be Threading) them, you're into a whole new ballgame - one that specialized companies and people have probably already solved far better than you (or I) ever will.

If this is simply a mental exercise, then by all means knock yourself out - there's nothing wrong with learning - but if this is for a proper application, then I suggest you heed some of the warnings you've been given.

That said, a simple LRU (least recently used) cache can be built quite easily with a LinkedHashMap. You might want to read the class docs for more information.

My 2¢

Winston
 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Winston. I never used any of the caching earlier
I am planning to do like below



Is this good approach?

Thank you for the suggestions.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hrithik khurana wrote:I am planning to do like below...

<sigh></sigh>

Is this good approach?

Assuming the data is pre-loaded, then it will probably do (although the HashMap should probably be typed); but, I'm not familiar with a 'WashingtonServlet', so I can't comment on anything that it might do or need.

However, if you plan to load the cache as you need values, then an LRU cache would probably be better - otherwise its size could easily mushroom - and for that, you really need a LinkedHashMap.

Winston
 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Winston



Rather than on load-on-startup =1

I want to invoke a servlet whenever http://google.com/ is called . Though http://google.com/ will not called literally.
Whenever http://google.com/Washington is called I want to invoke google.com's servlet before invoking Washington servlet.

Any help on how do I do this.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!