database backup and restore in sql server 2005

  • Need help in taking database backup and restore using java......

    Database: SQLServer 2005

    I have done it this way.....

    SQL Backup Query="BACKUP DATABASE mydatabase TO disk=?";

    while executing, iam getting the following error ....

    Cannot perform a backup or restore operation within a transaction.

    Also let me know, how to make a forced database backup?

  • Which driver are you using ?

    Are you (or the driver by default) using implicit transactions ?

    A backup/restore operation cannot be incapsulated with a transaction.

    If a restore fails, the db is left in its failed state (restoring) !

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • What do you mean by a "forced" database backup?

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • thanks for the reply.......

    Iam using type 4 driver (sqljdbc) .......

    If a query has to be executed, it has to be in transaction......

    Or else is there any other way?

    What do you mean by a "forced" database backup?

    Sometimes, while executing the query i have encountered the following error

    "some active connections.........."

    So before going ahead, i need to close the active connections and then proceed further.......So, in such cases is it possible to make forced backup and restore...

    Note: forced in the sense, complete the database operations irrespective of active connections

  • abhishek.c1984 (7/22/2009)


    ... "some active connections.........."

    So before going ahead, i need to close the active connections and then proceed further.......So, in such cases is it possible to make forced backup and restore...

    Note: forced in the sense, complete the database operations irrespective of active connections

    Probably it refers to open connections to the target database of your restore operation.

    If you want to restore with interrupting all connections,

    just start your restore statement by adding

    alter database yourdb set offline with rollback immediate ;

    restore database ...

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Ah, right. ALZDBA answered it. You're not forcing the backup, you're forcing the restore, but not even that really, you're just kicking all the users off the database.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • thanks for the reply,

    I have just executed the query in SQL Server 2005, it worked fine...but the database connection was broken

    -------

    Nonqualified transactions are being rolled back. Estimated rollback completion: 100%.

    Failed to restart the current database. The current database is switched to master.

    RESTORE DATABASE successfully processed 561 pages in 0.350 seconds (13.130 MB/sec).

    ---------------------

    But it failed to start the current database...

    Error:

    Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: socket write error

    Note: How can i use this query in the application?

    It is giving the following error

    Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: ALTER DATABASE statement not allowed within multi-statement transaction.

  • abhishek.c1984 (7/23/2009)


    thanks for the reply,

    I have just executed the query in SQL Server 2005, it worked fine...but the database connection was broken

    -------

    Nonqualified transactions are being rolled back. Estimated rollback completion: 100%.

    Failed to restart the current database. The current database is switched to master.

    RESTORE DATABASE successfully processed 561 pages in 0.350 seconds (13.130 MB/sec).

    ---------------------

    But it failed to start the current database...

    Error:

    Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: socket write error

    Note: How can i use this query in the application?

    It is giving the following error

    Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: ALTER DATABASE statement not allowed within multi-statement transaction.

    Don't use a transaction to perform backup or restore operations !!

    ( it does not have a transaction scope - there is no rollback feature for backup / restore !)

    Do not connect to the database you are trying to restore !

    Connect to master or tempdb to perform your database restore operation.

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • thanks for the reply.....

    Can you please let me know how can i connect to master or tempdb to perform database restore operation.

    Tell me how to perform this operation from the java application, without being connected to database

  • BP is always to specify the database name you want to connect to (don't rely on the logins default database ! A sql instance can host multiple databases !)

    Check out BOL for the jdbc driver: http://msdn.microsoft.com/en-us/library/ms378988%28SQL.90%29.aspx

    IMO you should provide as many info as you can in your connection !

    This will help with problem solving, monitoring, ...

    e.g.

    // Get connection

    DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());

    String url="jdbc:microsoft:sqlserver://yourip;DatabaseName="yourdb";

    Connection conn=DriverManager.getConnection(url,"username","password");

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • thanks for the reply.......

    I didnot get you...

    BP is always to specify the database name you want to connect to (don't rely on the logins default database ! A sql instance can host multiple databases !)

    Check out BOL for the jdbc driver: http://msdn.microsoft.com/en-us/library/ms378988%28SQL.90%29.aspx

    IMO you should provide as many info as you can in your connection !

    What is BP, BOL and IMO?

  • Best Practice, Books Online, In My Openion.

  • ok thanks for the reply....

    Actually iam using hibernate, it will take care of such activities...

    w.r.t your opinion i have create a sample JDBC connection as shown below, but iam getting exception

    -----------------

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    Connection con=DriverManager.getConnection

    ("jdbc:sqlserver://localhost\\sqlexpress:1433;databaseName=sample", "sa", "sa");

    Statement stmt=con.createStatement();

    stmt.execute("alter database jiffy set offline with rollback immediate");

    stmt.execute("RESTORE DATABASE jiffy FROM disk

    ='D:\\restore\\database22072009173644.bak'");

    -------------

  • abhishek.c1984 (7/23/2009)


    ok thanks for the reply....

    Actually iam using hibernate, it will take care of such activities...

    w.r.t your opinion i have create a sample JDBC connection as shown below, but iam getting exception

    -----------------

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    Connection con=DriverManager.getConnection

    ("jdbc:sqlserver://localhost\\sqlexpress:1433;databaseName=sample", "sa", "sa");

    Statement stmt=con.createStatement();

    stmt.execute("alter database jiffy set offline with rollback immediate");

    stmt.execute("RESTORE DATABASE jiffy FROM disk

    ='D:\\restore\\database22072009173644.bak'");

    -------------

    I'm not a JDBC user, but I think you should use:

    ("jdbc:sqlserver://localhost\sqlexpress:1433;databaseName=sample", "sa", "sa");

    or

    ("jdbc:sqlserver://localhost\sqlexpress;databaseName=sample", "sa", "sa");

    or

    ("jdbc:sqlserver://localhost:1433;databaseName=sample", "sa", "sa");

    Doublecheck the port number is correct (it will be used if specified !)

    BOL ref: http://msdn.microsoft.com/en-us/library/ms378428%28SQL.90%29.aspx

    Don't use the "sa" account in your applications !

    For admin purposes, prefer windows authentication !

    If windows authentication is not an option, create your own sql account and make that one member of the sysadmin group for sqlserver.

    btw, best is to execute both statements in the same batch !

    Statement stmt=con.createStatement();

    stmt.execute("alter database jiffy set offline with rollback immediate;

    RESTORE DATABASE jiffy FROM disk='D:\restore\database22072009173644.bak'");

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • thanks for the reply........

    Can you please tell me How to create a new sql account and make that one member of the sysadmin group for sqlserver?

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply