1

In a search application, I need to keep track of the files and their locations. Currently am using a database table for this, but since I have to connect to the db every time I need to retrieve such data, this is obviously not efficient. Is there a method I can load the table to memory and use it? I won't need to modify it while it's in the memory.

Thank You!

Izza
  • 2,389
  • 8
  • 38
  • 60

4 Answers4

2

If all you want to do is retrieve one table into memory you can do this with a single SELECT statement. You can build a collection like a Map from the ResultSet. After that get the information you want from the Map.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • +1. I also think this is the best way since I do not need to MODIFY it while it's in the memory. – Izza Jun 03 '11 at 08:52
  • 1
    Even if you did need to modify it, you can update the table and make the same update to your in-memory copy. – Peter Lawrey Jun 03 '11 at 15:11
1

You could populate any of the several Java databases out there that have an in-memory mode, like HSQLDB, Derby, or H2. You might also look at SQLite, which isn't specifically Java but has various Java connectors as described in this Q&A here on StackOverflow.

But you don't have to connect to a DB each time you need to query it, you can use a connection pool to manage a set of connections you can reuse. Since usually the main delay is establishing a connection, this can lead to quite lot per-query overhead.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You could also use one of caching products like Ehcache, Memcache, Coherence and many others. I have some knowledge in using Ehache. Configure Hibernate to cache a particular query or entity object or a POJO. All subsequent searches with same criteria will be fetched from cache.

I believe similar features are provided by other products as well.

Vijay
  • 385
  • 2
  • 7
0

Your sentence "I won't need to modify it while it's in the memory." does not reflect the title of your question, where you apparently want to modify an commit back your data after using it.

If you simply want to speedup your app, why don't you store your data in some kind of variable? Depending on your development tool, it could be some kind of session variable.

plang
  • 5,446
  • 3
  • 25
  • 36
  • Yes, I corrected that now. I do not need to modify it while it's in the table. Sorry for the mistake! – Izza Jun 03 '11 at 08:51