You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-dev@db.apache.org by Olav Sandstaa <Ol...@Sun.COM> on 2006/03/29 16:33:24 UTC

How to start and stop Derby from within a test

Is there a standard/simple way of starting and stopping the database
and network server from a test that is written using the test
framework? Preferably a way that is working for both embedded and
network client tests? I have looked around in a few test, but have not
found any particular easy way to do this.

The reason I want to be able to stop and start Derby from a test is
that I am writing some tests for Connection.isValid() (see JIRA 1090)
and need to test the behavior of this call against a stopped database. 

The test I work on is located in jdbc4/TestConnectionMethods.java. The
code that I have running right now do the job for me right now, but if
would be good to get some feedback on on whether there are simpler and
more standard ways of achieving the same. The current code looks like
this:

Embedded mode:
==============

Stop Derby:

        try {
            DriverManager.getConnection("jdbc:derby:;shutdown=true");
        } catch(SQLException e) {
            // Ignore any exceptions from shutdown
        }

"Problem": stops all Derby databases (no problem in my test)

Start Derby:

        /*
         * Start Derby by getting a new connection and check that
         * the new connection is valid.
         */
        try {
            conn = ij.startJBMS();
        } catch (Exception e) {
            System.out.println("FAIL: failed to re-start database: " +
                               "Unexpected exception: " + e);
        }

Comment: This is the way it should be.


Network client mode:
====================

Stop Derby:

        /*
         * Test on stopped DB: stop Derby
         */
        try {
            DriverManager.getConnection("jdbc:derby:;shutdown=true");
	 } catch (Exception e) {
             // Ignore any exceptions from shutdown
	 }

        // Stop the network server
        try {
            NetworkServerControl networkServer = new NetworkServerControl();
            networkServer.shutdown();
        } catch(Exception e) {
            System.out.println("INFO: network server shutdown returned: " + e);
        }

Start Derby:

        /*
         * Start the network server and get a new connection
         */
        try {
            // Start the network server - hard coded values (FixMe)
            NetworkServerControl networkServer = new NetworkServerControl(InetAddress.getByName("localhost"), 1527);
            networkServer.start(null);

            // Wait for the network server to start
            boolean started = false;
            int retries = 10;         // Max retries = max seconds to wait
            while (!started && retries > 0) {
		  try {
                    // Sleep 1 second and then ping the network server
		      Thread.sleep(1000);
                    networkServer.ping();

                    // If ping does not throw an exception the server has started
                    started = true;
                } catch(Exception e) {
                    retries--;
		  }
	     }

            // Check if we got a reply on ping
            if (!started) {
                System.out.println("FAIL: failed to start network server");
            }

            // Get a new connection to the database
            conn = ij.startJBMS();
        } catch (Exception e) {
            System.out.println("FAIL: failed to re-start database: " +
                               "Unexpected exception: " + e);
        }

There are currently some hard coded constants to localhost and 1527
that I could probably get rid of.

It would be very good if the test framework had utility functions for
doing these tasks that worked both for embedded and client server
tests (similar to ij.startJBMS()). There might be some, but I have not
found any. Any tips and comments on how to do this are appreciated.

I also had to disable the use of the security manager for the
test. With security manager, the last call to ij.startJBMS() gets the
following exception:

   Unexpected exception: java.sql.SQLException: DERBY SQL error: SQLCODE: -1, SQLSTATE: XJ040, SQLERRMC: Failed to start database 'wombat', see the next exception for details.::SQLSTATE: XJ001Java exception: 'access denied (java.io.FilePermission /export/home/tmp/derbyjdbc4/DerbyNetClient/TestConnectionMethods/wombat/log/logmirror.ctrl read): java.security.AccessControlException'.

I do not know why, but disabling the security manager made this go
away. I probably should find a better way to get around this.

..olav

Re: How to start and stop Derby from within a test

Posted by Olav Sandstaa <Ol...@Sun.COM>.
Deepa Remesh <dr...@gmail.com> wrote:
> The tests in the harness use ij.startJBMS to get initial connections.
> The connection is obtained by using the properties specified in the
> <test>_app.properties file. ij.startJBMS method can also be used for
> any subsequent connections which use same properties.
> 
> For any subsequent connections which need to use another set of
> attributes (e.g, shutdown), TestUtil.getConnection method can be used.
> This method was recently added and now works for all frameworks. I
> have added a comment to DERBY-950
> (http://issues.apache.org/jira/browse/DERBY-950). Please look at that
> also to see if it helps.

Deepa, Thanks for the information about TestUtil.getConnection for
stopping the database and for getting new connections. Using this
instead for my own implementation based on DriverManager.getConnection
simplified the test code and also made the security exception go away.

..olav

Re: How to start and stop Derby from within a test

Posted by Deepa Remesh <dr...@gmail.com>.
On 3/30/06, Olav Sandstaa <Ol...@sun.com> wrote:

> Anyway, the best solution to this problem would be to be able to reuse
> functionality that already might exist in the test framework. Any
> suggestions are appreciated.

The tests in the harness use ij.startJBMS to get initial connections.
The connection is obtained by using the properties specified in the
<test>_app.properties file. ij.startJBMS method can also be used for
any subsequent connections which use same properties.

For any subsequent connections which need to use another set of
attributes (e.g, shutdown), TestUtil.getConnection method can be used.
This method was recently added and now works for all frameworks. I
have added a comment to DERBY-950
(http://issues.apache.org/jira/browse/DERBY-950). Please look at that
also to see if it helps.

Thanks,
Deepa

Re: How to start and stop Derby from within a test

Posted by Olav Sandstaa <Ol...@Sun.COM>.
Suresh Thalamati <su...@gmail.com> wrote:
> By looking at the stack it looks like log/logmirror.ctrl  is not 
> getting accessed in the privileged block at  line 2940 in LogToFile.java.
> if (checkpointInstant == LogCounter.INVALID_LOG_INSTANT &&					 
> getMirrorControlFileName().exists())

Thanks for looking into this and explaining why this security exception occured. 
 
> Not sure how you got checkpointInstant to be INVALID , may be your 
> test program might have exited before a first valid checkpoint. Any 
> one fixing this bugs should make sure that is the case.

That might very well be the case. It occured early in on of the JDBC4
tests. The database had just been started and only a few operations
had been performed against it before I stopped it in probably not a
clean way.
 
> In any case  call to exists should have been 
> privExists(getMirrorControlFileName()).
> 
> 
> Please file a Jira entry  with information on how to reproduce this bug.

Thanks for filing the Jira. I will try to make a small test program
for reproducing the problem. After replacing the code I was using for
stopping the database with TestUtil.getConnection as proposed by Deepa
the security exception disappeared. I will try to reproduce it in a
small test.

..olav

Re: How to start and stop Derby from within a test

Posted by Suresh Thalamati <su...@gmail.com>.
Olav Sandstaa wrote:
> Rick Hillegas <Ri...@Sun.COM> wrote:
> 

> java.sql.SQLException: Java exception: 'access denied (java.io.FilePermission /export/home/tmp/derbyjdbc4/DerbyNetClient/TestConnectionMethods/wombat/log/logmirror.ctrl read): java.security.AccessControlException'.
> 	at java.security.AccessControlContext.checkPermission(AccessControlContext.java:321)
> 	at java.security.AccessController.checkPermission(AccessController.java:546)
> 	at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
> 	at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
> 	at java.io.File.exists(File.java:731)
> 	at org.apache.derby.impl.store.raw.log.LogToFile.boot(LogToFile.java:2940)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.boot(BaseMonitor.java:1996)
> 	at org.apache.derby.impl.services.monitor.TopService.bootModule(TopService.java:290)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(BaseMonitor.java:542)
> 	at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Monitor.java:418)
> 	at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.bootLogFactory(BaseDataFileFactory.java:1762)
> 	at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.setRawStoreFactory(BaseDataFileFactory.java:1218)
> 	at org.apache.derby.impl.store.raw.RawStore.boot(RawStore.java:250)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.boot(BaseMonitor.java:1996)
> 	at org.apache.derby.impl.services.monitor.TopService.bootModule(TopService.java:290)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(BaseMonitor.java:542)
> 	at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Monitor.java:418)
> 	at org.apache.derby.impl.store.access.RAMAccessManager.boot(RAMAccessManager.java:987)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.boot(BaseMonitor.java:1996)
> 	at org.apache.derby.impl.services.monitor.TopService.bootModule(TopService.java:290)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(BaseMonitor.java:542)
> 	at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Monitor.java:418)
> 	at org.apache.derby.impl.db.BasicDatabase.bootStore(BasicDatabase.java:738)
> 	at org.apache.derby.impl.db.BasicDatabase.boot(BasicDatabase.java:178)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.boot(BaseMonitor.java:1996)
> 	at org.apache.derby.impl.services.monitor.TopService.bootModule(TopService.java:290)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.bootService(BaseMonitor.java:1831)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.startProviderService(BaseMonitor.java:1697)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.findProviderAndStartService(BaseMonitor.java:1577)
> 	at org.apache.derby.impl.services.monitor.BaseMonitor.startPersistentService(BaseMonitor.java:990)
> 	at org.apache.derby.iapi.services.monitor.Monitor.startPersistentService(Monitor.java:541)
> 	at org.apache.derby.impl.jdbc.EmbedConnection.bootDatabase(EmbedConnection.java:1586)
> 	at org.apache.derby.impl.jdbc.EmbedConnection.<init>(EmbedConnection.java:216)
> 	at org.apache.derby.impl.jdbc.EmbedConnection30.<init>(EmbedConnection30.java:72)
> 	at org.apache.derby.impl.jdbc.EmbedConnection40.<init>(EmbedConnection40.java:48)
> 	at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Driver40.java:62)
> 	at org.apache.derby.jdbc.InternalDriver.connect(InternalDriver.java:199)
> 	at org.apache.derby.impl.drda.Database.makeConnection(Database.java:231)
> 	at org.apache.derby.impl.drda.DRDAConnThread.getConnFromDatabaseName(DRDAConnThread.java:1147)
> 	at org.apache.derby.impl.drda.DRDAConnThread.verifyUserIdPassword(DRDAConnThread.java:1125)
> 	at org.apache.derby.impl.drda.DRDAConnThread.parseSECCHK(DRDAConnThread.java:2709)
> 	at org.apache.derby.impl.drda.DRDAConnThread.parseDRDAConnection(DRDAConnThread.java:987)
> 	at org.apache.derby.impl.drda.DRDAConnThread.processCommands(DRDAConnThread.java:830)
> 	at org.apache.derby.impl.drda.DRDAConnThread.run(DRDAConnThread.java:236)
> 
> The security exception is raised when Derby tries to get access to the
> log/logmirror.ctrl file during the second startup of the database. I
> would have expected that since this file was created earlier during
> the initial startup of the test, the test should already have the
> required security permissions to access it during the second startup?
> 
> Anyway, the best solution to this problem would be to be able to reuse
> functionality that already might exist in the test framework. Any
> suggestions are appreciated.
> 
> Regards,
> Olav
> 

By looking at the stack it looks like log/logmirror.ctrl  is not 
getting accessed in the privileged block at  line 2940 in LogToFile.java.
if (checkpointInstant == LogCounter.INVALID_LOG_INSTANT &&					 
getMirrorControlFileName().exists())

Not sure how you got checkpointInstant to be INVALID , may be your 
test program might have exited before a first valid checkpoint. Any 
one fixing this bugs should make sure that is the case.

In any case  call to exists should have been 
privExists(getMirrorControlFileName()).


Please file a Jira entry  with information on how to reproduce this bug.


Thanks
-suresh



Re: How to start and stop Derby from within a test

Posted by Myrna van Lunteren <m....@gmail.com>.
On 3/30/06, Myrna van Lunteren <m....@gmail.com> wrote:
>
>  I am also wondering if you're walking into DERBY-1069.
>

No, that's different, sorry.
Myrna

Re: How to start and stop Derby from within a test

Posted by Myrna van Lunteren <m....@gmail.com>.
On 3/30/06, Olav Sandstaa <Ol...@sun.com> wrote:
>
> Rick Hillegas <Ri...@Sun.COM> wrote:
> > Somewhere in the test output there should be a detailed stack trace for
> > this exception (you may have to run the test with verbose=true).  That
> > stack trace will point you at a piece of code which may need to be
> > wrapped in a privileged block. [snip] You may also need to add some
> permissions to the policy file used by
> > derbyall tests: derby_tests.policy.
>
> Rick, thanks for the hints on how to get the stack trace for where the
> security exception occurs and hints on how to fix it. I still hope
> that I do not have to do changes related to the security manager and I
> am still hoping for some hints for functionality in the test framework
> or code other tests on how to start and stop Derby in a less ad hoc
> way than I am doing it in this test.


[snip]

The security exception is raised when Derby tries to get access to the
> log/logmirror.ctrl file during the second startup of the database. I
> would have expected that since this file was created earlier during
> the initial startup of the test, the test should already have the
> required security permissions to access it during the second startup?


[snip]

Regards,
> Olav


As Deepa said, look at TestUtil.getConnection(<dbname>,<propertiesstring>).
I am also wondering if you're walking into DERBY-1069, and the problem is
actually some more basic trouble with making the connection.
See also
http://www.nabble.com/-jira-Created%3A-%28DERBY-1069%29-Client-should-unwrap-exceptions-thrown-in-privilege-blocks-rather-than-throwing-the-java.security.PrivilegedActionException-t1201470.html#a3170997
on
this.

Myrna

Re: How to start and stop Derby from within a test

Posted by Olav Sandstaa <Ol...@Sun.COM>.
Rick Hillegas <Ri...@Sun.COM> wrote:
> Somewhere in the test output there should be a detailed stack trace for 
> this exception (you may have to run the test with verbose=true).  That 
> stack trace will point you at a piece of code which may need to be 
> wrapped in a privileged block. You can grep the codeline for 
> "AccessController.doPrivileged" for example of how to code one of these. 
> You may also need to add some permissions to the policy file used by 
> derbyall tests: derby_tests.policy.

Rick, thanks for the hints on how to get the stack trace for where the
security exception occurs and hints on how to fix it. I still hope
that I do not have to do changes related to the security manager and I
am still hoping for some hints for functionality in the test framework
or code other tests on how to start and stop Derby in a less ad hoc
way than I am doing it in this test.

Anyway, here is the stack trace for the security exception:

java.sql.SQLException: Failed to start database 'wombat', see the next exception for details.
	at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(SQLExceptionFactory40.java:66)
	at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Util.java:87)
	at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Util.java:93)
	at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Util.java:172)
	at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(EmbedConnection.java:1939)
	at org.apache.derby.impl.jdbc.EmbedConnection.bootDatabase(EmbedConnection.java:1603)
	at org.apache.derby.impl.jdbc.EmbedConnection.<init>(EmbedConnection.java:216)
	at org.apache.derby.impl.jdbc.EmbedConnection30.<init>(EmbedConnection30.java:72)
	at org.apache.derby.impl.jdbc.EmbedConnection40.<init>(EmbedConnection40.java:48)
	at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Driver40.java:62)
	at org.apache.derby.jdbc.InternalDriver.connect(InternalDriver.java:199)
	at org.apache.derby.impl.drda.Database.makeConnection(Database.java:231)
	at org.apache.derby.impl.drda.DRDAConnThread.getConnFromDatabaseName(DRDAConnThread.java:1147)
	at org.apache.derby.impl.drda.DRDAConnThread.verifyUserIdPassword(DRDAConnThread.java:1125)
	at org.apache.derby.impl.drda.DRDAConnThread.parseSECCHK(DRDAConnThread.java:2709)
	at org.apache.derby.impl.drda.DRDAConnThread.parseDRDAConnection(DRDAConnThread.java:987)
	at org.apache.derby.impl.drda.DRDAConnThread.processCommands(DRDAConnThread.java:830)
	at org.apache.derby.impl.drda.DRDAConnThread.run(DRDAConnThread.java:236)
java.sql.SQLException: Java exception: 'access denied (java.io.FilePermission /export/home/tmp/derbyjdbc4/DerbyNetClient/TestConnectionMethods/wombat/log/logmirror.ctrl read): java.security.AccessControlException'.
	at java.security.AccessControlContext.checkPermission(AccessControlContext.java:321)
	at java.security.AccessController.checkPermission(AccessController.java:546)
	at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
	at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
	at java.io.File.exists(File.java:731)
	at org.apache.derby.impl.store.raw.log.LogToFile.boot(LogToFile.java:2940)
	at org.apache.derby.impl.services.monitor.BaseMonitor.boot(BaseMonitor.java:1996)
	at org.apache.derby.impl.services.monitor.TopService.bootModule(TopService.java:290)
	at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(BaseMonitor.java:542)
	at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Monitor.java:418)
	at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.bootLogFactory(BaseDataFileFactory.java:1762)
	at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.setRawStoreFactory(BaseDataFileFactory.java:1218)
	at org.apache.derby.impl.store.raw.RawStore.boot(RawStore.java:250)
	at org.apache.derby.impl.services.monitor.BaseMonitor.boot(BaseMonitor.java:1996)
	at org.apache.derby.impl.services.monitor.TopService.bootModule(TopService.java:290)
	at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(BaseMonitor.java:542)
	at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Monitor.java:418)
	at org.apache.derby.impl.store.access.RAMAccessManager.boot(RAMAccessManager.java:987)
	at org.apache.derby.impl.services.monitor.BaseMonitor.boot(BaseMonitor.java:1996)
	at org.apache.derby.impl.services.monitor.TopService.bootModule(TopService.java:290)
	at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(BaseMonitor.java:542)
	at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Monitor.java:418)
	at org.apache.derby.impl.db.BasicDatabase.bootStore(BasicDatabase.java:738)
	at org.apache.derby.impl.db.BasicDatabase.boot(BasicDatabase.java:178)
	at org.apache.derby.impl.services.monitor.BaseMonitor.boot(BaseMonitor.java:1996)
	at org.apache.derby.impl.services.monitor.TopService.bootModule(TopService.java:290)
	at org.apache.derby.impl.services.monitor.BaseMonitor.bootService(BaseMonitor.java:1831)
	at org.apache.derby.impl.services.monitor.BaseMonitor.startProviderService(BaseMonitor.java:1697)
	at org.apache.derby.impl.services.monitor.BaseMonitor.findProviderAndStartService(BaseMonitor.java:1577)
	at org.apache.derby.impl.services.monitor.BaseMonitor.startPersistentService(BaseMonitor.java:990)
	at org.apache.derby.iapi.services.monitor.Monitor.startPersistentService(Monitor.java:541)
	at org.apache.derby.impl.jdbc.EmbedConnection.bootDatabase(EmbedConnection.java:1586)
	at org.apache.derby.impl.jdbc.EmbedConnection.<init>(EmbedConnection.java:216)
	at org.apache.derby.impl.jdbc.EmbedConnection30.<init>(EmbedConnection30.java:72)
	at org.apache.derby.impl.jdbc.EmbedConnection40.<init>(EmbedConnection40.java:48)
	at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Driver40.java:62)
	at org.apache.derby.jdbc.InternalDriver.connect(InternalDriver.java:199)
	at org.apache.derby.impl.drda.Database.makeConnection(Database.java:231)
	at org.apache.derby.impl.drda.DRDAConnThread.getConnFromDatabaseName(DRDAConnThread.java:1147)
	at org.apache.derby.impl.drda.DRDAConnThread.verifyUserIdPassword(DRDAConnThread.java:1125)
	at org.apache.derby.impl.drda.DRDAConnThread.parseSECCHK(DRDAConnThread.java:2709)
	at org.apache.derby.impl.drda.DRDAConnThread.parseDRDAConnection(DRDAConnThread.java:987)
	at org.apache.derby.impl.drda.DRDAConnThread.processCommands(DRDAConnThread.java:830)
	at org.apache.derby.impl.drda.DRDAConnThread.run(DRDAConnThread.java:236)

The security exception is raised when Derby tries to get access to the
log/logmirror.ctrl file during the second startup of the database. I
would have expected that since this file was created earlier during
the initial startup of the test, the test should already have the
required security permissions to access it during the second startup?

Anyway, the best solution to this problem would be to be able to reuse
functionality that already might exist in the test framework. Any
suggestions are appreciated.

Regards,
Olav

Re: How to start and stop Derby from within a test

Posted by Rick Hillegas <Ri...@Sun.COM>.
Olav Sandstaa wrote:

...

>I also had to disable the use of the security manager for the
>test. With security manager, the last call to ij.startJBMS() gets the
>following exception:
>
>   Unexpected exception: java.sql.SQLException: DERBY SQL error: SQLCODE: -1, SQLSTATE: XJ040, SQLERRMC: Failed to start database 'wombat', see the next exception for details.::SQLSTATE: XJ001Java exception: 'access denied (java.io.FilePermission /export/home/tmp/derbyjdbc4/DerbyNetClient/TestConnectionMethods/wombat/log/logmirror.ctrl read): java.security.AccessControlException'.
>
>I do not know why, but disabling the security manager made this go
>away. I probably should find a better way to get around this.
>
>  
>
Somewhere in the test output there should be a detailed stack trace for 
this exception (you may have to run the test with verbose=true).  That 
stack trace will point you at a piece of code which may need to be 
wrapped in a privileged block. You can grep the codeline for 
"AccessController.doPrivileged" for example of how to code one of these. 
You may also need to add some permissions to the policy file used by 
derbyall tests: derby_tests.policy.