You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Randy Watler <wa...@wispertel.net> on 2006/03/13 21:36:44 UTC

Accessing embedded Derby in forked Maven2 build

All,

I am running into difficulties finding a way to shutdown a Derby 
database, (10.1.1.0), from within an Ant build invoked from Maven2. I 
need to do this to release a database lock for the running JVM so that 
subsequently forked tests in their own JVMs can access the database. 
Here is what I tried:

        <!--
            Tests will be executed in forked JVM process while this
            Ant build is executed in the main Maven process; the Derby
            embedded database must be shutdown to prevent JVM instance
            level locking that would prevent the tests from running
        -->
        <sql url="jdbc:derby:/tmp/testdb;shutdown=true"
             driver="org.apache.derby.jdbc.EmbeddedDriver" 
             userid="" password=""
             onerror="continue" >
            <classpath>
                <fileset refid="derby.embedded.db.dependencies.id"/>
            </classpath>
            -- SHUTDOWN DERBY EMBEDDED DATABASE TO FREE JVM LOCK 
        </sql>

Unfortunately, the onerror attribute does not apply to the initial 
connection establishment phase, just the execution of SQL statements. 
This task resulted in the following error:

Embedded error: The following error occurred while executing this line:
C:\jetspeed-2\etc\build.xml:258: The following error occurred while executing this line:
C:\jetspeed-2\etc\build.xml:357: SQL Exception: Database '/tmp/testdb' shutdown.

What I'd really like to have is an SQL statement I can execute to 
perform the shutdown. Barring that, finding a way to avoid throwing the 
connect SQLException might also work. A Derby Ant task to perform the 
shutdown would be very handy as well if it is available in the Derby 
distribution. Are any of these or other options available that I might try?

I'd really like to find a way to shutdown the database from Ant. This 
approach fits the general architecture of the existing Jetspeed2 build. 
Because we have not needed to delve into custom Ant tasks or Maven2 
plugins to date, I'd rather not go there either.

Thanks for any input you can provide in advance,

Randy Watler, (Jetspeed 2 committer)


Re: Accessing embedded Derby in forked Maven2 build

Posted by Randy Watler <wa...@wispertel.net>.
Andrew/Jean,

I have resolved this by forking the build process to place the sql 
operations against the DB in a separate JVM process. This way, the build 
will sequentially startup individual JVMs for all derby access and 
thereby avoid the locks.

To make life easier for Ant and other similarly challenged environments, 
it might be nice to have a statement, (e.g. stored procedure), of some 
sort that can shutdown the databases and engines. Of course, I 
understand that may not even be possible... :-(.

Anyway, off and running with a clunky patch of Ant code for now... 
thanks for the help!

Randy

Randy Watler wrote:
> Jean T. Anderson wrote:
>> This developerWorks article provides StopDatabaseTask and StopDerbyTask
>> Ant tasks:
>>
>> http://www.ibm.com/developerworks/db2/library/techarticle/dm-0412snell/index.html 
>>
>>
>> If this helps resolve your problem, please let us know.
>>
>>  -jean
>>
>>   
> I've been trying hard to avoid the custom tasks and the build fun that 
> involves. Thanks for the reference though... might just come in handy 
> if i can not manage to fork ant from ant...
>
> Randy
>
>
>


Re: Accessing embedded Derby in forked Maven2 build

Posted by Randy Watler <wa...@wispertel.net>.
Jean T. Anderson wrote:
> This developerWorks article provides StopDatabaseTask and StopDerbyTask
> Ant tasks:
>
> http://www.ibm.com/developerworks/db2/library/techarticle/dm-0412snell/index.html
>
> If this helps resolve your problem, please let us know.
>
>  -jean
>
>   
I've been trying hard to avoid the custom tasks and the build fun that 
involves. Thanks for the reference though... might just come in handy if 
i can not manage to fork ant from ant...

Randy


Re: Accessing embedded Derby in forked Maven2 build

Posted by "Jean T. Anderson" <jt...@bristowhill.com>.
Randy Watler wrote:
> Andrew:
> 
> Andrew McIntyre wrote:
> 
>>
>> It sounds like you're setting up a test database using Ant's <sql>
>> commands and this leaves the Derby engine running in the same VM as
>> Ant. So, my original solution wouldn't work, as ij wouldn't be able to
>> connect. It sounds like you may need to move the creation of the test
>> database into another VM. You could do this by putting all of the SQL
>> you have in your Ant <sql> statements into a file and running it in a
>> forked JVM using ij.
>>
>>   
> 
> Right. This is why when I run your solution I get cannot connect
> messages... even if it is not forked. So how is the best way to shutdown
> the engine? I assume that because an engine is running, ij cannot
> connect. But why is that if I do not fork ij?
> 
> Randy
> 
> 

This developerWorks article provides StopDatabaseTask and StopDerbyTask
Ant tasks:

http://www.ibm.com/developerworks/db2/library/techarticle/dm-0412snell/index.html

If this helps resolve your problem, please let us know.

 -jean

Re: Accessing embedded Derby in forked Maven2 build

Posted by Randy Watler <wa...@wispertel.net>.
Andrew,

Below are the errors I am getting w/o a fork... not sure why this is and 
how ij knows there is another connection from this same JVM, (albeit 
probably a different classloader)?

ij version 10.1
ij> connect 'jdbc:derby:/tmp/testdb';
ERROR XJ040: Failed to start database '/tmp/testdb', see the next 
exception for details.
ERROR XSDB6: Another instance of Derby may have already booted the 
database C:\tmp\testdb.
ij> connect 'jdbc:derby:/tmp/testdb;shutdown=true';
ERROR XJ004: Database '/tmp/testdb' not found.


Randy


Re: Accessing embedded Derby in forked Maven2 build

Posted by Randy Watler <wa...@wispertel.net>.
Andrew:

Andrew McIntyre wrote:
>
> It sounds like you're setting up a test database using Ant's <sql>
> commands and this leaves the Derby engine running in the same VM as
> Ant. So, my original solution wouldn't work, as ij wouldn't be able to
> connect. It sounds like you may need to move the creation of the test
> database into another VM. You could do this by putting all of the SQL
> you have in your Ant <sql> statements into a file and running it in a
> forked JVM using ij.
>
>   
Right. This is why when I run your solution I get cannot connect 
messages... even if it is not forked. So how is the best way to shutdown 
the engine? I assume that because an engine is running, ij cannot 
connect. But why is that if I do not fork ij?

Randy



Re: Accessing embedded Derby in forked Maven2 build

Posted by Andrew McIntyre <mc...@gmail.com>.
On 3/13/06, Randy Watler <wa...@wispertel.net> wrote:
> Andrew,
>
> Thanks for the speedy reply. Let me give this a try.
>
> To be honest, I am confused about the use of the fork="true" attribute
> in the <java> task. Can shutdown=true be invoked from another JVM
> instance like this and have it release the lock that the parent JVM is
> holding?
>
> Randy

I had to fork the VM because I wanted the task to run in a specific
location relative to the basedir of the Ant project, which requires
that fork be set to true. This assumed that another instance of the
engine wasn't running, just that Derby wasn't shutdown cleanly when
the previous JVM connected to Derby and did its work. The reason for
that is that the same scripts are used to test the build of the
database, and shutting down Derby from inside the scripts which load
the tables would prevent the test from completing.

It sounds like you're setting up a test database using Ant's <sql>
commands and this leaves the Derby engine running in the same VM as
Ant. So, my original solution wouldn't work, as ij wouldn't be able to
connect. It sounds like you may need to move the creation of the test
database into another VM. You could do this by putting all of the SQL
you have in your Ant <sql> statements into a file and running it in a
forked JVM using ij.

andrew

Re: Accessing embedded Derby in forked Maven2 build

Posted by Randy Watler <wa...@wispertel.net>.
Andrew,

Thanks for the speedy reply. Let me give this a try.

To be honest, I am confused about the use of the fork="true" attribute 
in the <java> task. Can shutdown=true be invoked from another JVM 
instance like this and have it release the lock that the parent JVM is 
holding?

Randy

Andrew McIntyre wrote:
> On 3/13/06, Randy Watler <wa...@wispertel.net> wrote:
>   
>> What I'd really like to have is an SQL statement I can execute to
>> perform the shutdown. Barring that, finding a way to avoid throwing the
>> connect SQLException might also work. A Derby Ant task to perform the
>> shutdown would be very handy as well if it is available in the Derby
>> distribution. Are any of these or other options available that I might try?
>>     
>
> There isn't currently a Derby Ant task, but feel free to contribute
> one if you have the time. I recently wanted to do the same thing for
> an Ant build.xml that builds a sample database for Derby (see
> DERBY-422 in JIRA), and I used ij:
>
>     <java classname="org.apache.derby.tools.ij"
>           append="true"
>           failonerror="true"
>           dir="." fork="true">
>       <classpath>
>         <pathelement path="${lib.dir}/derby.jar"/>
>         <pathelement path="${lib.dir}/derbytools.jar"/>
>       </classpath>
>       <arg value="shutdown.sql"/>
>     </java>
>
> Where the contents of shutdown.sql is:
>
> connect 'jdbc:derby:toursdb';
> connect 'jdbc:derby:toursdb;shutdown=true';
>
> Would that do the trick?
>
> andrew
>
>
>   


Re: Accessing embedded Derby in forked Maven2 build

Posted by Andrew McIntyre <mc...@gmail.com>.
On 3/13/06, Randy Watler <wa...@wispertel.net> wrote:
>
> What I'd really like to have is an SQL statement I can execute to
> perform the shutdown. Barring that, finding a way to avoid throwing the
> connect SQLException might also work. A Derby Ant task to perform the
> shutdown would be very handy as well if it is available in the Derby
> distribution. Are any of these or other options available that I might try?

There isn't currently a Derby Ant task, but feel free to contribute
one if you have the time. I recently wanted to do the same thing for
an Ant build.xml that builds a sample database for Derby (see
DERBY-422 in JIRA), and I used ij:

    <java classname="org.apache.derby.tools.ij"
          append="true"
          failonerror="true"
          dir="." fork="true">
      <classpath>
        <pathelement path="${lib.dir}/derby.jar"/>
        <pathelement path="${lib.dir}/derbytools.jar"/>
      </classpath>
      <arg value="shutdown.sql"/>
    </java>

Where the contents of shutdown.sql is:

connect 'jdbc:derby:toursdb';
connect 'jdbc:derby:toursdb;shutdown=true';

Would that do the trick?

andrew