3

I heared a lot that reading and writing a file from Java EE environment (e.g. Glassfish Server) is bad design and a bad decision. Is it always true?

I know that it´s not good because multiple threads could access one file. I want to create a dynamic property file for my application. There only one or two admins who will adjust the properties. So I could persist the properties in the database, but the simpler way is to store it in the file.

So is the simpler way a bad decision and what are the disadvantages?

ovonel
  • 232
  • 3
  • 12

2 Answers2

2

As you probably know, writing to (and reading from) a file within an EJB is prohibited by the specification, since Files aren't transactional resources, and therefore cannot be managed by the container. So if you want to be on the safe side, don't do it. That said, many still do it without problems, including many common third party libraries that require configuration. Still, I would consider putting it in the database. It won't be that much harder.

Edit

My answer was written assuming you were talking about doing it in an EJB container, which of course might not be the case after all when I read your question again. If it's just a simple web application, no such restriction in the specification exists. But you might find it useful to put it in the database anyway, since it's easier synchronizing incremental updates to a database than to a file.

NilsH
  • 13,705
  • 4
  • 41
  • 59
  • I am handling about 2GB of static images, wouldn't that be pretty harsh on the database? – Menno May 07 '13 at 10:05
  • @Aquillo I was assuming the OP was talking about an EJB container, which I see now might not be the case. If it's just a simple web application, I would answer differently. I'll update my answer to clarify the scope. – NilsH May 07 '13 at 10:14
  • Well I'm using a webapplication with EJB features, though I need to handle static images in this application too, hence the question :) – Menno May 07 '13 at 10:16
  • 1
    Another thing to consider is whether the application (ejb or not) will be clustered. If an application relies on files written to the local filesystem, then it should be for settings local to that specific instance. If it's clustered across multiple machines and the data needs to apply across all instances in the cluster, writing in a file is not appropriate. That is a potential disadvantage. – EdH May 07 '13 at 11:02
1

This is not a bad decision in that matter.

As you mentioned storing files by container as a normal user case is a bad decision, because it is:

  1. Slow
  2. Not thread safe.

But the case you provided is rather configuration and not normal usage, so I cannot see drawbacks of this.

But anyway you should be careful and use some kind of mutual exclusion not to store file by two admins at once.

Michal Borek
  • 4,584
  • 2
  • 30
  • 40