开发者

Where should a Java web application store its data?

开发者 https://www.devze.com 2023-01-05 15:37 出处:网络
My Java web application (myapp.war) ist deployed by placing it into the webapps direcotry on Tomcat on Ubuntu 10.04.

My Java web application (myapp.war) ist deployed by placing it into the webapps direcotry on Tomcat on Ubuntu 10.04.

This application needs to save some data in files. But the user, which is running Tomcat (tomcat6) has no write access to the home directory /usr/share/tomcat6/ and no write access to the current working direcotry /var/lib/tomcat6/, since both belong to root.

So where should a web application store its data? I hope it is not the extracted archive in the we开发者_开发问答bapps direcotry. This one could be deleted very easily by accident. And Tomcat can be configured, not to extract .war files. Then there would be no extracted direcotry.

Perhaps /var/lib/tomcat6/ should belong to user tomcat6 ant this is a bug in Ubuntu?


If the files need not persist longer than the life-cycle of the servlet context, the servlet container provides a private temporary directory for each servlet context, specified by javax.servlet.context.tempdir attribute.

See Servlet Specification 2.3, Chapter 3 Servlet Context

3.7.1 Temporary Working Directories

The convenience of a temporary storage directory is required for each servlet context. Servlet containers must provide a private temporary directory per servlet context and make it available via the javax.servlet.context.tempdircontext attribute. The object associated with the attribute must be of type java.io.File


Answering his own question, Witek stated /var/lib/tomcat6/webapps/ is writable -- at least on his installation of his version of Ubuntu. On my RHEL 5.2 system /var/lib/tomcat<X> doesn't even exist, so there is no webapps subdirectory writable or not, which leads to my answer.

Q: Where should a Java web application store its data?
A: Wherever you've configured it to store its data.

Make the location configurable, in web.xml as a <context-param> or in a myApplication.properties file.

  • I can put it where I want on my box, the SysAdmins can put it where they want on the production system.
  • You can change your mind later.
  • You don't need symbolic links (which have magically disappeared on me before, breaking the system.)
  • You can have several sets of test data, and just point the configuration at whichever one you want.
  • You can put it wherever there's disk space.
  • You are going to change your mind later.


I think it depends on what kind of data you are dealing with. Most of the time, data goes into the database simply because it is fast and easy to perform a CRUD. If you want to store localized user configuration and you don't care how portable it is, perhaps you can store under user.home, I did that for one of my projects and that works fine. All that being said, I really don't think there's any best practice on this and database seems to be the most obvious choice because you can do whole lot of different tasks against it, and most of them are free to begin with. :)


I found the solution on Launchpad. /var/lib/tomcat6/webapps/ is writable. This means, that the following works:

File myFile = new File("webapps/myfile.txt");


I haven't seen any specific guidance on where you should store that kind of data locally - probably because you'd normally store that kind of data in a database.

When I need to store quick-and-dirty data like that, I store it on a filesystem specifically for that data, to isolate it from other areas where it might get caught up in other activity. Haven't had any issues with the approach so far. Also, if it's important, make sure you store it somewhere where you're backing it up :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号