Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

Ankur Luthra

Ranch Hand
+ Follow
since Dec 13, 2010
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ankur Luthra

A.J. Côté wrote:Hello,

Strange requirement. Are your tables read-only? If not you will need to hit the database for writes anyway.

Anyway, for read-only tables, I would probably just copy the db to an in-memory database or use a simple one file based database engine with the file kept on a RAM drive.


Could you please explain in detail above point?

A.J. Côté wrote:
You should need to fine tune the size of the memory cache available to hibernate/Ehcache anyway so, by making it bigger or big enough, most of the cacheable data would be cached anyway. Why should you insist on having *everything* in the cache?



The reason is it contains mostly static data and there is no point in hitting the Db again and again for the same data. Although data may be asked in different forms but it's mostly going to be static.
Updates are very few .Probably 10-12 updates in a day.I am relying on Ehcache to update the cache when I update any row in Db via session.saveOrUpdate call.
4 years ago

Claude Moore wrote:

Ankur Luthra wrote: I need to serve the requests using Cache and not hit Database at all.


May I ask you: why ? Is it by design ? I'm not an expert of Ehcache, but generally speaking the main purpose of a cache is NOT to keep all data in memory, only more accessed ones. Depending on how much is your database large, your approach may even never be feasible.



Hi,
Yup ,it's by design. Since this Db is mostly read only with very less updates/writes .Also it mostly contains static data hence the reason.
4 years ago
The technology stack I am using is Spring + Hibernate + Ehcache. I need to serve the requests using Cache and not hit Database at all. The approach I am following is to cache the whole data at App Startup using queries such as getSession().createCriteria(.class).list() and then ask Hibernate for entities using session .get() call so that it uses second level cache to resolve stuff.

One challenge I see is my entity classes have a lot of Collection and Association Attributes(Many to Many with extra columns ,One to Many, One to One).

I have two approaches for such cache.

a)Keep all relationships as EAGER fetch.So the cache will be filled with the data with a Big query containing Left Outer Joins on App Startup. I am concerned that loading data eagerly may cause unnecessary long running query returning multiple rows.

b) Keep relationships as LAZY and iterate over all the rows and call .getSetOf to load related entities. I am concerned that I will be virtually iterating whole data at App startup and not sure if it's a good practice.

Since the associations in Hibernate are associated using composition and foreign keys and not stored as individual Ids hence loading of such associations/Collections seems to be an overhead.

I would have preferred having table data in different cache regions with their foreign key relationships as just Ids (not compositions/collections) .I would have cached all such entities in different regions and would have combined the results at run time by iterating over those regions.

Can anyone suggest what approach should I follow.If there is any alternate approach,do suggest.

Thanks in Advance.

P.S:- Posted the same question on SF . Need to make an urgent Decision hence posting duplicate question.
http://stackoverflow.com/questions/33068249/what-is-a-good-way-to-cache-whole-database-using-spring-hibernate-and-ehcache
4 years ago
Hi,
I am using following way to create sessionFactory.

I want to use JNDI Name configured on Tomcat Server for instantiating the Data Source.

So I created another Bean

When I use ,I am not able to instantiate the DataSource and get following Exception.

Please let me know what am I doing wrong.

Regards,
Ankur
5 years ago
I am using Spring in my project and instantiating dataSource as below.


Below is my properties file.


Please suggest :-
a)Any changes in the properties marked in Bold(initialSize,maxActive,maxidle,minIdle). My application will be concurrently used by around 100 users and total users are around 3000.
b)I am using Tomcat Server to deploy my application.Should I be using JNDI for connections instead of directly specify connection properties? Is above way of using connections good for a production System?

P.S.
I have asked same question @ SF.
http://stackoverflow.com/questions/26230531/need-inputs-for-database-tuning-using-spring-and-dbcp-connection-pool

Regards,
Ankur
5 years ago
The problem is geronimo*activation*.jar contains the package javax.activation and I see javax.activation as a dependency for commons-email.
hence the question.
6 years ago
Hi,
I have added mail-1.4.jar in my class path and it works.
Is there any issue if i keep geronimo-javamail_1.4_spec-1.7.1.jar and geronimo-activation_1.1_spec-1.1.jar also in my classpath?

Regards,
Ankur

6 years ago
Hi,
I am facing some issue while using commons-mail API.

Getting following Exception .
Caused by: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
at javax.mail.Session.getProvider(Session.java:237)
at javax.mail.Session.getTransport(Session.java:346)
at javax.mail.Session.getTransport(Session.java:376)
at javax.mail.Transport.send(Transport.java:67)
at javax.mail.Transport.send(Transport.java:48)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
... 3 more


Below is snippet of Code that I am using


6 years ago
I am using quartz and Spring for Scheduling some Task. I have two methods as below which are exposed as REST GET methods and when request with path as"../startSchedulor" OR "../stopSchedulor" is made, I start OR stop scheduler



This REST service is deployed along with other REST services in the project.Now when i will deploy the project in a cluster with say 5 servers and I hit /startSchedulor and the request goes to say server-1, it will start scheduler on server-1.Now when I will hit /stopSchedulor it is not guaranteed that server-1 will take the request and stop my schedulor. How can i make sure that my scheduler is stopped when i hit /stopSchedulor URL.

One way I can think of is to package the REST service separately and deploy it only on one server.Any other way to overcome this?

P.S. :- same has been posted on Stack Overflow "http://stackoverflow.com/questions/25911077/suggestions-for-starting-schdeulor-on-one-server-in-a-cluster";

Regards,
Ankur
6 years ago
Hi,
I need to know if I can implement the above code in a more elegant way.
Any re-factoring that can be done.

Should I be making the class a singleton?
Most of the variables in this class will remain constant except TO-Address ,Subject and Body.
Any other way by which I can make sure that all variables except (TO-Address ,Subject and Body) gets instantiated only once.

Regards,
Ankur
6 years ago
Same topic has been posted on StackOverFlow. Need inputs from fellow Code Ranchers too.
http://stackoverflow.com/questions/25545226/javamail-code-suggestions

I am writing a Bean which can send same message to multiple user or can send different message to same user. My application is REST based and an email needs to be triggered when a service is hit.Application can send any number of emails in a day. I have written below class for the same.Any improvements that I can do in below code. I fetch values for properties like from ,host,etc from my Db.I am querying the Db only once and populating javax.mail. Session object with the same.

6 years ago

Chan Ag wrote:I'm signing off for the day cause I need to get back to my assignments. There are other nicer, wiser people around and I'm sure they'll help you. But you might want to give a thought to the things I said.

Take a break of at least some 15-20 minutes before posting again. :-) And in that period, reflect on your requirement. You might get a deeper understanding of it by doing that.

I'm sure you will be able to solve things yourself/or with minimal additional help.

Chan.




Thanks Chan for all your time.
Have a great day.:-)
7 years ago

Chan Ag wrote:

Init() can called one time by multiple Threads using different instances of Test Class. I get your point that in every Thread ,code is recreating the static HashMap.

So if I initialize the HashMap it in the beginning ,do you think issue will not occur.

i.e. something like, ................................



Like I said, forget about the ConcurrentModificationException now. There are many basic things that we may need fix here. They are easy to understand but let's do it one step at a time.

So like I said before, what is your requirement. Not how many threads, how many objects and stuff.. but what you want to do.

Like - 'I want to create a map that contains some key value pairs. This map needs to be accessible by all objects of my class. But I need to initialize the map only once. .. and stuff.. '
I'm not sure though if that is your requirement.

But, if you do this-

Init() can called one time by multiple Threads using different instances of Test Class.



Do you realize what you're doing when each of your thread is attempting to ( let's imagine there was no ConcurrentModificationException ) re-create a similar map in every invocation. Even if your init method was a static method, wouldn't that be a pointless thing to do?

So like I said, come up with reasons on why you're doing what you are doing.

Chan.



Chan I agree with every thing you said above.
I didn't write the code but have been given the dirty work of finding out what could be going wrong with the code.
This is the exact requirement that you mentioned above
"I want to create a map that contains some key value pairs. This map needs to be accessible by all objects of my class. But I need to initialize the map only once. .. and stuff.. '" and I know its pointless to re create similar Map fir every Thread invocation but wanted to fix this without shuffling code much.

How about initializing in the Static block.

7 years ago

Chan Ag wrote:How about we begin with you stating your requirement clearly so people might be able to suggest better.



My requirement is to call init method.
I need to somehow have this Map populated with key as testStr[i] and value as new Integer(0) in the method.

a) This method can be called by multiple threads one time using the same instance of Test class.
b) This method can be called by multiple threads one time using the different instances of Test class.

Can i get this population of Map somehow Thread Safe?
7 years ago

Chan Ag wrote:

If you see the Test class ,the static Map is used inside synchronized instance method init().So if two threads try to call this method using the same object they can never enter together .One has to wait for other to leave and the only place this object is modified is inside this synchronized instance method.

So can we rule out the situation when this method is called using same instance of Test Class.?

The only case when this exception could occur is when two threads try to call init() method using different instances of Test .Right?



That would be right given what you said.

But I'm wondering why are you doing what you are doing? Did you intend to call the init method only once? If yes, you are clearly not doing that.

If you intend to call the init method multiple times, why are you recreating the static map from a static array?

Chan.




Init() can called one time by multiple Threads using different instances of Test Class. I get your point that in every Thread ,code is recreating the static HashMap.

So if I initialize the HashMap it in the beginning ,do you think issue will not occur.

i.e. something like,



Here also one Thread may try to access the HashMap and other is putting into the Map ,but because the keys(testStr[i]) are same ,so ideally no modification of Collection is happening.
Can you may be suggest how can I make safe from Exception.One way suggested was making Map to ConcurrentHashMap.
7 years ago