0

I have a java spring MVC web application build using spring xml configuration, deployed on tomcat server.

My requirement is, whenever the tomcat server starts or get restarted after shut down,I want to clean one table from the database before deploying the war. The database used is mysql.

I have two approaches in my mind right now, one is to keep the table that needs cleanup, in memory using some in memory database. Second one is to make tomcat server to execute the script (that will take care of cleaning the data from table) before deploying the war. But I am looking for a solution that my application can itself take care of cleaning the table before the war deployment. Is there any solution available using spring? Or any other solutions?

Thanks!

Reena Upadhyay
  • 1,977
  • 20
  • 35

5 Answers5

0

use a WebApplicationInitializer as per Spring Docs.

or Refer these answers:

  1. Spring Web Application: Post-DispatcherServlet initialization
  2. spring mvc servlet initialization

Sample code using ServletContextListener (from 2nd answer)

Add your listener to your application's web.xml

Eg:

<listener>
   <listener-class>InitListener</listener-class>
</listener>

sample code:

public class InitListener implements javax.servlet.ServletContextListener {

   public void contextInitialized(ServletContext context) {
      Database.clearTable();
   }
}
Community
  • 1
  • 1
Arun Xavier
  • 763
  • 8
  • 47
0

Note:- This solution is a work around:- I suggest to deploy that from the system administration level, where you can trigger a cron job/windows task to check if tomcat service is running or not(for manual start) OR check tomcat logs for last date/time it was restarted(automatic start) this might need some configurations, then compare that with the closest date/time accordingly you can fire your own job/task to truncate your mysql table.

0

IMHO a very simple way is to use a non lazy Spring singleton bean with an init method that cleans the table. You can easily makes it depend on a Datasource bean, and Spring ensures that the init method will be called before the web application can answer its first request.

class Cleaner {

    Datasource datasource;
    void init() {
       // clean the table
    }
    void setDatasource(Datasource datasource) {
        this.datasource = datasource;
    }
}

with the following declaration in root context:

<bean id="cleaner" class="...Cleaner" p:datasource="datasource" init-method="init"/>
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

We use liquibase to version our scripts.

Takes a bit to setup but after that you can version all your scripts and configure some that run every time you execute liquibase's update command.

We use Jenkins CI server to deploy our war files, which simply runs liquibase before deploying a war. This ensures the database is up-to date on all scripts and you can also execute scripts every build.

Not sure what you are using, but there are maven and gradle plugins for liquibase.

RadoTonev
  • 45
  • 1
  • 7
0

Read below link, hopefully you get the answer: http://crunchify.com/how-to-run-java-program-automatically-on-tomcat-startup/