java 在哪里/如何使用tomcat存储持久数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1792634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:00:42  来源:igfitidea点击:

Where/how to store persistent data with tomcat?

javatomcat

提问by nos

Where should I store persistent files in a Tomcat web app ?

我应该将持久文件存储在 Tomcat Web 应用程序中的什么位置?

  • javax.servlet.context.tempdir is not feasible, it's erased when the app is redeployed/removed
  • Don't want to use an absolute path in e.g. servlet init parameters
  • Storing the files in a database is not an option
  • javax.servlet.context.tempdir 不可行,在重新部署/删除应用程序时将其删除
  • 不想在例如 servlet init 参数中使用绝对路径
  • 将文件存储在数据库中不是一种选择

采纳答案by Andy Gherna

Our team does this a lot. A general rule we follow is outside the web app and outside Tomcat.

我们的团队经常这样做。我们遵循的一般规则是在 Web 应用程序之外和 Tomcat 之外。

Our sysadmin set up a directory on our server that the tomcat user has rw permissions to (e.g. /var/tomcat/persist). We have a built a directory structure under this that tomcat uses to store files, read app-specific init files, etc.

我们的系统管理员在我们的服务器上设置了一个目录,tomcat 用户具有 rw 权限(例如/var/tomcat/persist)。我们在这个目录下构建了一个目录结构,tomcat 使用它来存储文件、读取特定于应用程序的 init 文件等。

If you don't want to use an absolute path in your init-params for your servlet, consider setting a system property when tomcat is started up. The good thing about that is every application running under tomcat will have access to it. The bad thing about that is every application running under tomcat will have access to it. You could set a property named base.persist.dirand build subdirectories for each application underneath it. We set system properties in the setenv.shscript in the bin/directory under the CATALINA_OPTS environment variable.

如果不想在 servlet 的 init-params 中使用绝对路径,请考虑在启动 tomcat 时设置系统属性。这样做的好处是每个在 tomcat 下运行的应用程序都可以访问它。坏处是在 tomcat 下运行的每个应用程序都可以访问它。您可以base.persist.dir为其下的每个应用程序设置一个名为的属性并构建子目录。我们在CATALINA_OPTS 环境变量下bin/目录下的setenv.sh脚本中设置系统属性。

回答by Pascal Thivent

Answering the title of the question, what about using a database, a DataSourceand JDNI? Even in a web only context, writing to files using java.iois not really recommended because of concurrency, threading, security, clustering, portability issues. Some of these problems can be "workarounded" but still, this is not really a best practice. The standard approach is to use a database and I'd suggest to reconsider this option, throwing "file-based" lightweight database like HSQLBD or JavaDB into the mix.

回答问题的标题,使用数据库,aDataSource和JDNI怎么样?即使在仅限 Web 的上下文中,java.io由于并发、线程、安全、集群、可移植性问题,也不推荐使用写入文件。其中一些问题可以“解决”,但这仍然不是真正的最佳实践。标准方法是使用数据库,我建议重新考虑这个选项,将“基于文件的”轻量级数据库(如 HSQLBD 或 JavaDB)混为一谈。

(EDIT: For an unknown reason, database is not an option. Using JNDI or context parameters or init parameters to pass an absolutepath - which are the less worse options IMHO - is excluded too. For a relativepath, maybe look at user.homeor user.dirthen - or any other system property that you could pass on the command line. I don't like it, I wouldn't do it, and this doesn't solve the issues previously mentioned, but it's your choice after all.)

(编辑:出于未知原因,数据库不是一个选项。使用 JNDI 或上下文参数或 init 参数传递绝对路径 - 恕我直言,这是不太糟糕的选项 - 也被排除在外。对于相对路径,可以查看user.homeuser.dir然后- 或您可以在命令行上传递的任何其他系统属性。我不喜欢它,我不会这样做,这并不能解决前面提到的问题,但毕竟这是您的选择。)

回答by yossis

Storing the files in a webapp directory under the home directory of the user running Tomcat is a good and convenient option. It is outside of Tomcat, which means it will survive redeployment, and it is usually a writable directory (because it is created under the users' home dir). But it is always a good idea to allow overriding the location of such directory via system property.

将文件存储在运行 Tomcat 的用户的主目录下的 webapp 目录中是一个很好且方便的选择。它在 Tomcat 之外,这意味着它将在重新部署后继续存在,并且它通常是一个可写目录(因为它是在用户的主目录下创建的)。但允许通过系统属性覆盖此类目录的位置总是一个好主意。

回答by mhaller

Generally, this would go to the database. But since the OP insists on not using a database, I'd try a different approach:

通常,这将转到数据库。但由于 OP 坚持不使用数据库,我会尝试不同的方法:

  • Filesystem path which is known: ${user.home}/.myapp. Applications sometimes use this for e.g. search indices which can be recalculated based on data in the database. Might be okay for your use case to use the user's home.
  • Store the configurable filesystem path in a configuration repository such as the database or perhaps Java Preferences(if you don't like to use servlet init params). Commercial applications such as Atlassian JIRA use a configurable (but absolute) filesystem path where they store issue attachments. If they don't know a better way, i don't know who does :)
  • 已知的文件系统路径:${user.home}/.myapp. 应用程序有时将此用于例如可以根据数据库中的数据重新计算的搜索索引。您的用例可以使用user's home
  • 将可配置的文件系统路径存储在配置存储库中,例如数据库或Java 首选项(如果您不喜欢使用 servlet init 参数)。Atlassian JIRA 等商业应用程序使用可配置(但绝对)的文件系统路径,用于存储问题附件。如果他们不知道更好的方法,我不知道谁知道:)

回答by Suppressingfire

I generally would suggest to use a database to store persistent data and expose it via a DataSource.

我通常会建议使用数据库来存储持久数据并通过数据源公开它。

If you don't want to do that, I guess you could consider using the "user.home" system property (I have seen this used in a few circumstances). But... there are no guarantees that your servlet will be run with permission to write access unless you configure that yourself.

如果您不想这样做,我想您可以考虑使用“user.home”系统属性(我已经看到在一些情况下使用过)。但是……除非您自己配置,否则无法保证您的 servlet 将在具有写访问权限的情况下运行。