Abhinav Garg
Abhinav Garg

Reputation: 1692

How to clean database without stopping tomcat using java

I am using sql server and tomcat for my web application To refresh/clean my database i have to close tomcat for successful cleanup Is there any way using java that i can clean my database without closing tomcat

Upvotes: 1

Views: 566

Answers (3)

anon
anon

Reputation:

You can kill external connections to your database using:

USE master;
GO
ALTER DATABASE dbname SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
USE dbname;
GO
-- now you can perform your "database cleanup" as you are the only
-- user allowed in.

When you are done your cleanup:

USE master;
GO
ALTER DATABASE dbname SET MULTI_USER;
GO

Now, what happens to the users who try to access the application during this time, and how tomcat reacts, <shrug>. Seems a lot cleaner to just shut tomcat down, depending on what you want the users to see during cleanup.

Upvotes: 2

Bohemian
Bohemian

Reputation: 425013

You must close all database connections that your tomcat webapps have held open.

To do this without stopping tomcat, you have to somehow tell your webapps to either temporarily shutdown their db connections while the database is being refreshed (too much hassle) or just simply shutdown the webapps that use database connections by removing them from the webapps folder.

A combination of the two techniques would be best: signal to your webapps to shutdown, then kill them.

Upvotes: 0

Ben
Ben

Reputation: 13625

You can execute SQL commands from your java code like DELETE and DROP TABLE

Upvotes: 0

Related Questions