Tuesday, February 20, 2018

Karaf Shell Scripting Caveats - Part II

New learnings on the Karaf Shell Scripting front. I find new caveats every day - hopefully those leanings can help my visitors save some time. If you haven't do check out some of the other caveats at: Karaf Shell Scripting Caveats - Part I

Today's learning: Script statements may return asynchronously!
At least for jdbc:ds-create for Karaf v4.0.9. This means you should not assume that when a statement returns, that call is all done.

As an example consider the following 2 lines of script:
    jdbc:ds-create -dn mysql -url "jdbc:mysql://$hostName:3306/information_schema?user=$username&password=$password" $hostName\.information_schema ;    dbList = (jdbc:query $hostName\.information_schema "select schema_name from schemata"  |  tac) ;
The first statement creates a JDBC DataSource. The second statement uses that datasource to run a query against the database.

For cases where the script was running on a machine with the target MySQL database running locally - the call to jdbc:ds-create returned very quickly, so the second call always succeeded.

When the database was not local, network latencies etc meant that it took longer for the datasource creation process to be finish. As a result, the second statement failed as it could not find a named datasource to use.

A simple (but not the best) solution is to add a sleep statement in between the two. A better solution would be to write code to check for the existence of the new datasource before trying to using it - something to work on in the future...


No comments: