简体   繁体   中英

Avoid Tomcat to delete deployed webapp folder

I have an Spring MVC app that uses an embedded DB to store users credentials and settings. My problem is that I decided to store DB files inside the deployed webapp folder of Tomcat dynamically. Today I realized that this folder is deleted every time Tomcat is restarted (or I think so). My question is, is there a way to avoid Tomcat to delete the folder? If not, where can I store database files? Which would be the better path if the app is aimed for Windows, Linux and MacOS?

Tomcat never deletes folders from webapp on restart by itself. It will delete if anything special is configured in shut down script/ startup script. Restarting tomcat is just redeploying the files present in the webapp. Please check if your program which is creating database files is actually storing the same, means please check the files after shut down. If they exist , then there is something written in your startup script which might be deleting those files

Also storing database files in tomcat webapp is not a good option as any subsequent deployment will remove the war and redeploy it, thus erasing all the prior data.

Also depending on the size of the data you can chose to store data directly to DB.

It is not good Idea to store in tomcat dir,during deploy may delete that I recommended Redis to do it http://www.springsource.org/spring-data/redis ,Or if you want store in embedded DB create a directory file inside home of user that is using both windows and unix by

System.getProperty("user.home");

Or you may get tomcat home directory If CATALINA_HOME set in environment variable by

System.getProperty("catalina.base");

Or you can use absolute path with using spring Message bundle ,so you can add dir.properties file and add in spring config:

  <bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/dir" />
    <property name="defaultEncoding" value="UTF-8" />

and inside file dir.properties ,add path:

    storage:/var/lib/tomcat6/

Or ,if you are intrested to find Os dynamically, you can use this code to get specific dir:

   public class FindOS {

private static final boolean osIsMacOsX;
private static final boolean osIsWindows;
private static final boolean osIsWindowsXP;
private static final boolean osIsWindows2003;
private static final boolean osIsWindowsVista;
private static final boolean osIsLinux;

static {
String os = System.getProperty("os.name");
if (os != null)
os = os.toLowerCase();  
osIsMacOsX = "mac os x".equals(os);
osIsWindows = os != null && os.indexOf("windows") != -1;
osIsWindowsXP = "windows xp".equals(os);
osIsWindows2003 = "windows 2003".equals(os);
osIsWindowsVista = "windows vista".equals(os);
osIsLinux = os != null && os.indexOf("linux") != -1;
}

public static boolean isMacOSX() {
return osIsMacOsX;
}

public static boolean isWindows() {
return osIsWindows;
    }

public static boolean isWindowsXP() {
return osIsWindowsXP;
}

public static boolean isWindows2003() {
return osIsWindows2003;
}

public static boolean isWindowsVista() {
return osIsWindowsVista;
}

public static boolean isLinux() {
return osIsLinux;
}

//TODO
String getHelperDirectory(){    
    if(isLinux())return "~/";       
    if(isWindows()) return "c:/";
    if(osIsWindowsVista) return "c:/";
    return null;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM