【发布时间】:2018-07-26 08:24:44
【问题描述】:
我在本地部署了一个构建,我可以使用https://localhost:8443/dashboard 访问该应用程序。现在,作为我的 junit 的一部分,我应该备份此应用程序使用的数据库并将其恢复到其早期状态,因为应用程序在多个表中插入一些数据作为测试的一部分。所以,我需要在测试运行之前恢复数据库。现在,我可以使用java程序(junit)备份和恢复它,但是当服务器运行时问题就来了。它说DB is already in use and can't restore。有没有办法断开应用程序中使用的数据库连接(用户),进行恢复然后重新连接,这样我就不必关闭服务器并再次手动启动它。
使用类似于以下的命令-
BACKUP DATABASE [Store] TO DISK = N'"+backuppath+"' WITH NOFORMAT, NOINIT,
NAME = 'demodb-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10";
和
RESTORE DATABASE ["+dbName+"] FROM DISK='"+database_backup_location+"' WITH
REPLACE, MOVE '"+mdfLogicalName+"' TO
'"+getPropertyByKey("databse.dbfiles.location")+mdfLogicalName+".mdf', MOVE
'"+ldfLogicalName+"' TO
'"+getPropertyByKey("databse.dbfiles.location")+ldfLogicalName+".ldf';
我得到的错误 -
org.springframework.jdbc.UncategorizedSQLException: StatementCallback;
uncategorized SQLException for SQL [RESTORE DATABASE ....error code [3101];
Exclusive access could not be obtained because the database is in use.; nested
exception is com.microsoft.sqlserver.jdbc.SQLServerException: Exclusive access
could not be obtained because the database is in use.
【问题讨论】:
-
SQL Server有BACKUP和RESTORE命令,备份是不中断工作的,你为什么不想用呢?
-
>>>有没有办法断开应用程序中使用的数据库连接(用户)
-
@sepupic,我已经在使用 BACKUP 和 RESTORE 命令。 RESTORE 命令给出错误,因为数据库正在使用中。这就是我需要克服的。也更新了我的问题中的命令。
标签: java sql-server database