2

I need to run an Apache Derby Server as a service so Derby starts together with the machine it is installed on.

Now I tried three solutions -based on my research- to create such a service but none of them are either feasible or generate errors.

  1. Use the Java Service Wrapper: this is not feasible because they only offer a 30 day trial for free.

  2. Use the Windows Server Manager (SrvMgr.exe) to create a service as described here: http://www.vogella.com/tutorials/ApacheDerby/article.html. This seems to work because I created a service that shows up in the Windows Registry, can be started/stopped via the Services panel and something is using http://localhost:1527 the default address for the Derby server. However if I try to connect to the database via eclipse (luna) or the console I get an error:

    Could not connect to DerbyPersistenceDeploy.
    Error creating SQL Model Connection connection to DerbyPersistenceDeploy. 
    (Error: DERBY SQL error: ERRORCODE: 40000, SQLSTATE: XJ041, SQLERRMC: 
    Failed to create database 'persistence', see the next exception for
    details.::SQLSTATE: XBM0J)
    DERBY SQL error: ERRORCODE: 40000, SQLSTATE: XJ041, SQLERRMC: 
    Failed to create database 'persistence', see the next exception for
    details.::SQLSTATE: XBM0J
    Error creating JDBC Connection connection to DerbyPersistenceDeploy. 
    (Error: DERBY SQL error: ERRORCODE: 40000, SQLSTATE: XJ041, SQLERRMC: 
    Failed to create database 'persistence', see the next exception for 
    details.::SQLSTATE: XBM0J)
    DERBY SQL error: ERRORCODE: 40000, SQLSTATE: XJ041, SQLERRMC: 
    Failed to create database 'persistence', see the next exception for             
    details.::SQLSTATE: XBM0J 
    

    Here is an image of the Registry entry of the service: Registry Entry

  3. The last approach I tied was to use Apache procrun (http://commons.apache.org/proper/commons-daemon/procrun.html). I worked my way through this tutorial (http://joerglenhard.wordpress.com/2012/05/29/build-windows-service-from-java-application-with-procrun/) and I managed to get the example service running. However as I tried to adapt the script posted in the tuorial I got the following error (from the log file):

    [2014-07-21 16:52:20] [error] [ 3708] 
    Method 'static void start(String[])' not found in Class org/apache/derby/drda/NetworkServerControl
    [2014-07-21 16:52:20] [error] [ 6228] 
    Failed to start Java
    [2014-07-21 16:52:20] [error] [ 6228] 
    ServiceStart returned 4
    [2014-07-21 16:52:20] [error] [ 6228] 
    Commons Daemon procrun failed with exit value: 3 (Failed to run service as console application)
    

    Here is my adapted script to create the webservice:

    set SERVICE_NAME=DerbyPersistenceService
    set PR_INSTALL=D:\Program-Files\commons-daemon-1.0.15-bin-windows\prunsrv.exe
    
    REM Service log configuration
    set PR_LOGPREFIX=%SERVICE_NAME%
    set PR_LOGPATH=D:\04_server\derby\DerbyServiceScript\logs
    set PR_STDOUTPUT=D:\04_server\derby\DerbyServiceScript\logs\stdout.txt
    set PR_STDERROR=D:\04_server\derby\DerbyServiceScript\logs\stderr.txt
    set PR_LOGLEVEL=Error
    
    REM Path to java installation
    set PR_JVM=C:\Program Files (x86)\Java\jdk1.7.0_65\jre\bin\client\jvm.dll
    set PR_CLASSPATH=%DERBY_HOME%/lib/derby.jar;
                     %DERBY_HOME%/lib/derbynet.jar;
                     %DERBY_HOME%/lib/derbyclient.jar;
                     %DERBY_HOME%/lib/derbytools.jar
    
    REM Startup configuration
    set PR_STARTUP=auto
    set PR_STARTMODE=jvm
    set PR_STARTCLASS=org.apache.derby.drda.NetworkServerControl
    set PR_STARTPARAM=start
    set PR_STARTMETHOD=main
    
    REM Shutdown configuration
    set PR_STOPMODE=jvm
    set PR_STOPCLASS=org.apache.derby.drda.NetworkServerControl
    set PR_STOPPARAM=shutdown
    set PR_STOPMETHOD=main
    
    REM JVM configuration
    set PR_JVMMS=256
    set PR_JVMMX=1024
    set PR_JVMSS=4000
    set PR_JVMOPTIONS=-Duser.language=US;-Duser.region=en
    REM Install service 
    D:\Program-Files\commons-daemon-1.0.15-bin-windows\prunsrv.exe //IS//%SERVICE_NAME%
    

    I assume that this error stems from the fact that the Startup- and Shutdown-Configurations are not configured the right way but I have no idea what they should look like.

If someone knows a solution for either approach 2. or 3. I would be very grateful.

Greetings

[edit1:] I edited my Procrun script as Bryan Pendleton suggested.

[edit2:]The Service gives me a new error message: error message 2

stderr.txt:

    2014-07-23 16:41:14 Commons Daemon procrun stderr initialized

stdout.txt:

    2014-07-23 16:41:14 Commons Daemon procrun stdout initialized
    Wed Jul 23 16:41:14 CEST 2014 : No command given.
    Usage: NetworkServerControl <commands> 
    Commands:
    start [-h <host>] [-p <port number>] [-noSecurityManager] [-ssl <ssl mode>]
    shutdown [-h <host>][-p <port number>] [-ssl <ssl mode>] [-user <username>] [-password <password>]
    ping [-h <host>][-p <port number>] [-ssl <ssl mode>]
    sysinfo [-h <host>][-p <port number>] [-ssl <ssl mode>]
    runtimeinfo [-h <host>][-p <port number>] [-ssl <ssl mode>]
    logconnections { on|off } [-h <host>][-p <port number>] [-ssl <ssl mode>]
    maxthreads <max>[-h <host>][-p <port number>] [-ssl <ssl mode>]
    timeslice <milliseconds>[-h <host>][-p <port number>] [-ssl <ssl mode>]
    trace { on|off } [-s <session id>][-h <host>][-p <port number>] [-ssl <ssl mode>]
    tracedirectory <trace directory>[-h <host>][-p <port number>] [-ssl <ssl mode>]

DerbyPersistenceService.2014-07-23.log is empty.

I think something is not right with the method arguments.

[edit3:]Changed the script so it is a working solution. Many thanks to Bryan Pendleton for helping me with this one.

    set SERVICE_NAME=DerbyPersistenceService
    set PR_INSTALL=D:\Program-Files\commons-daemon-1.0.15-bin-windows\prunsrv.exe

    REM Service log configuration
    set PR_LOGPREFIX=%SERVICE_NAME%
    set PR_LOGPATH=D:\04_server\derby\DerbyServiceScript\logs
    set PR_STDOUTPUT=D:\04_server\derby\DerbyServiceScript\logs\stdout.txt
    set PR_STDERROR=D:\04_server\derby\DerbyServiceScript\logs\stderr.txt
    set PR_LOGLEVEL=Error

    REM Path to java installation
    set PR_JVM=C:\Program Files (x86)\Java\jdk1.7.0_65\jre\bin\client\jvm.dll
    set PR_CLASSPATH=%DERBY_HOME%/lib/derby.jar;
                     %DERBY_HOME%/lib/derbynet.jar;
                     %DERBY_HOME%/lib/derbyclient.jar;
                     %DERBY_HOME%/lib/derbytools.jar

    REM Startup configuration
    set PR_STARTUP=auto
    set PR_STARTMODE=jvm
    set PR_STARTCLASS=org.apache.derby.drda.NetworkServerControl
    set PR_STARTPARAMS=start
    set PR_STARTMETHOD=main

    REM Shutdown configuration
    set PR_STOPMODE=jvm
    set PR_STOPCLASS=org.apache.derby.drda.NetworkServerControl
    set PR_STOPPARAMS=shutdown
    set PR_STOPMETHOD=main

    REM JVM configuration
    set PR_JVMMS=256
    set PR_JVMMX=1024
    set PR_JVMSS=4000
    set PR_JVMOPTIONS=-Duser.language=US;-Duser.region=en
    REM Install service 
    D:\Program-Files\commons-daemon-1.0.15-bin-windows\prunsrv.exe //IS//%SERVICE_NAME%  
Scyla
  • 199
  • 3
  • 14
  • What did you get in these files: set PR_LOGPATH=D:\04_server\derby\DerbyServiceScript\logs set PR_STDOUTPUT=D:\04_server\derby\DerbyServiceScript\logs\stdout.txt set PR_STDERROR=D:\04_server\derby\DerbyServiceScript\logs\stderr.txt – Bryan Pendleton Jul 23 '14 at 13:53
  • Ok so I looked into the log files. The error code 4 stems from the fact that the old service was still installed. After I uninstalled the previous service and installed the new one with the updated script I'm getting a different error message. Will update the post – Scyla Jul 23 '14 at 14:36
  • The documentation for procrun seems to indicate it's PR_STARTPARAMS, not PR_STARTPARAM. Did you try setting PR_STARTPARAMS? – Bryan Pendleton Jul 28 '14 at 20:35
  • Thank you it is now working. I accepted your solution. Stupid mistake on my side. – Scyla Jul 29 '14 at 06:59

2 Answers2

2

I believe the PR_STARTMETHOD and PR_STOPMETHOD should be 'main', because you want to call NetworkServerControl's main() method.

I believe that 'start' and 'shutdown' are arguments that you need to pass to the main() method.

So they should go in STARTPARAMS and STOPPARAMS, respectively.

Bryan Pendleton
  • 16,128
  • 3
  • 32
  • 56
  • Thank you for your response. I tried to work your suggestion into my solution but I still get an error message (see updated post above). – Scyla Jul 23 '14 at 07:31
0

I was in the same problem and try with a wide variety of tools and wrappers to get Derby as a windows service, among them all the ways depicted in Derby wiki (http://wiki.apache.org/db-derby/DerbyWindowsService).

Anyway, none of these tools worked for me except for procrun tools as provided by Appache foundation, which is used in tomcat to get this server as a windows server. So, please following the next steps:

  1. Get the two key files of procrun (http://commons.apache.org/proper/commons-daemon/binaries.html):
    • prunsrv.exe - service application for running applications as services.
    • prunmgr.exe - the GUI manager application used to monitor and configure installed services.
  2. Copy these files at Derby/bin (supposing you have a folder named Derby which is the DERBY_HOME).
  3. To following the procrun convention, rename these files to derby.exe and derbyw.exe. If you want to use different names you can but you should then adapt the attached script.
  4. To create the service at windows you have to execute the next script. It was writen to be executed on up-level folder (..) of Derby folder, and by using a portable version of java 32 bits.

    • NOTE: I've split it on different lines, but it has to be executed in only one line

    Derby\bin\derby.exe //IS//DerbyService
    --DisplayName="Derby Service"
    --Description="This is a Derby databse server"
    --Install="%cd%\Derby\bin\derby.exe" --Startup=auto
    --JavaHome "%cd%\Java_32_portable"
    --Jvm="%cd%\Java_32_portable\bin\client\jvm.dll"
    --StartMode=Java --StopMode=Java
    --StartClass=org.apache.derby.drda.NetworkServerControl --StartParams=start
    --StopClass=org.apache.derby.drda.NetworkServerControl --StopParams=shutdown
    --Classpath="%cd%\Derby\lib\derby.jar;%cd%\Derby\lib\derbyrun.jar;%cd%\Derby\lib\derbynet.jar;%cd%\Derby\lib\derbytools.jar"
    ++JvmOptions="-Dderby.system.home=%cd%\Derby;-Dderby.install.url=%cd%\Derby\lib;-Dderby.authentication.provider=BUILTIN;-Dderby.storage.pageCacheSize=8000;-Dderby.storage.pageSize=20000;-Dderby.database.sqlAuthorization=false"

  5. IMPORTANT. You should adapt bold parts.

    • Set your JAVA_HOME variable and your jvm.dll appropriately
    • Set your own Derby specific parameters within ++JvmOptions separated by ';'. For example, add user-pasword autentication with:
      -Dderby.database.sqlAuthorization=true;-Dderby.user."user-name"=password
  6. Finally, you can check you service starts well with

    net start DerbyService

Advice: you can put all toghether in a bat file as I have, where you can define previously JAVA_HOME or any other necessary variable.

Richard P.
  • 703
  • 7
  • 9