0

I've written a small web application that allows the client to POST values and then GET the sum of those values, using a glassfish server in intellij. I want the sum to persist across service restarts. Currently, the sum is being tracked with a Singleton resource so that it can be shared with a couple different classes. Is there a way that I can write and read a file in a local directory? Something like:

src:
     Singleton
     Rest of classes
res:
     PersistentDataFile
FutureShocked
  • 779
  • 1
  • 10
  • 26

1 Answers1

0

Yes, You can write the data in a local file, but it should not be in resources directory. Because, if you it in resources directory, it will become part of your WAR file and later, if you deploy updated WAR file, this file will gets override.

Now there are 2 solutions -

1) Store data in a file placed outside of your web application. However, you need to handle multi-threading and you have to write code for avoiding read/write of the file by more one thread i.e. in more than 1 request.

2) Recommended Approach - For handling such situations, storing such data in database is recommended approach. In this approach also, you need to handle modification of data by more than one thread. However, such situations can be easily handled by writing appropriate SQL query by which read and write can be done in a single query (not always possible) or maintain some version column or last modified timestamp column in database.

Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26