Detect if the database is open
Wednesday, 15 June 2016 · 6 messages · last reply 20 Jun 2016
KS
Kendall Shaw15 Jun 2016 19:03 #
Using ClientSession from java, I do this to check if the database is open:
try {
replace(somewhere, new ArrayInput(bytes));
} catch (IOException e) {
String msg = e.getMessage();
if (msg == null || !msg.contains(“No database opened”))
throw e;
execute(new Check(databaseName));
replace(somewhere, new ArrayInput(bytes));
}Is there a better way to detect if the database is open than testing the exception message text?
Kendall
CG
Christian Grün18 Jun 2016 13:54 #
Hi Kendall,
I would indeed recommend you to use the CHECK command before doing any other operations. This command does exactly what you seem to be looking for (checking if the databases is opened; if not, open it).
Does this help?
Christian
Quoted text · Kendall Shaw · 32 lines
On Wed, Jun 15, 2016 at 7:03 PM, Kendall Shaw <kendall.shaw at workday.com> wrote: > Using ClientSession from java, I do this to check if the database is open: > > > > try { > > replace(somewhere, new ArrayInput(bytes)); > > } catch (IOException e) { > String msg = e.getMessage(); > > if (msg == null || !msg.contains(“No database opened”)) > > throw e; > > execute(new Check(databaseName)); > > replace(somewhere, new ArrayInput(bytes)); > > } > > > > Is there a better way to detect if the database is open than testing the > exception message text? > > > > Kendall > >
KS
Kendall Shaw19 Jun 2016 23:59 #
Hi,
I think of opening a database, in general, as something you wouldn’t want to do every time you do any operation. I would expect:
open the database
do stuff
do stuff
do stuff
do stuff
do stuff
close the database
not
open the database and do stuff
open the database and do stuff
open the database and do stuff
open the database and do stuff
close the database
Is using CHECK when the database is already open comparable in CPU cycles to testing a boolean variable that says open true or false? I understand that the time it takes for most commands to execute would make code logic usually insignificant.
Kendall
Quoted text · Christian Grün · 44 lines
On 6/18/16, 4:54 AM, "Christian Grün" <christian.gruen at gmail.com> wrote: >Hi Kendall, > >I would indeed recommend you to use the CHECK command before doing any >other operations. This command does exactly what you seem to be >looking for (checking if the databases is opened; if not, open it). > >Does this help? >Christian > > > >On Wed, Jun 15, 2016 at 7:03 PM, Kendall Shaw <kendall.shaw at workday.com> wrote: >> Using ClientSession from java, I do this to check if the database is open: >> >> >> >> try { >> >> replace(somewhere, new ArrayInput(bytes)); >> >> } catch (IOException e) { >> String msg = e.getMessage(); >> >> if (msg == null || !msg.contains(“No database opened”)) >> >> throw e; >> >> execute(new Check(databaseName)); >> >> replace(somewhere, new ArrayInput(bytes)); >> >> } >> >> >> >> Is there a better way to detect if the database is open than testing the >> exception message text? >> >> >> >> Kendall >> >>
CG
Christian Grün20 Jun 2016 00:25 #
Hi Kendall,
Quoted text · Kendall Shaw · 1 line
> Is using CHECK when the database is already open comparable in CPU cycles to testing a boolean variable that says open true or false? I understand that the time it takes for most commands to execute would make code logic usually insignificant.
Check will definitely be slower than a pure boolean check. On the other hand, I wouldn’t expect you to notice any delay, as the execution time should only be a few milliseconds, or even less.
What will be the processing time you require for this operation? There may be some chance to improve the execution time of this command (it just hasn’t been a bottleneck so far).
Cheers,
Christian
Quoted text · Christian Grün · 46 lines
> On 6/18/16, 4:54 AM, "Christian Grün" <christian.gruen at gmail.com> wrote: > >>Hi Kendall, >> >>I would indeed recommend you to use the CHECK command before doing any >>other operations. This command does exactly what you seem to be >>looking for (checking if the databases is opened; if not, open it). >> >>Does this help? >>Christian >> >> >> >>On Wed, Jun 15, 2016 at 7:03 PM, Kendall Shaw <kendall.shaw at workday.com> wrote: >>> Using ClientSession from java, I do this to check if the database is open: >>> >>> >>> >>> try { >>> >>> replace(somewhere, new ArrayInput(bytes)); >>> >>> } catch (IOException e) { >>> String msg = e.getMessage(); >>> >>> if (msg == null || !msg.contains(“No database opened”)) >>> >>> throw e; >>> >>> execute(new Check(databaseName)); >>> >>> replace(somewhere, new ArrayInput(bytes)); >>> >>> } >>> >>> >>> >>> Is there a better way to detect if the database is open than testing the >>> exception message text? >>> >>> >>> >>> Kendall >>> >>> >
KS
Kendall Shaw20 Jun 2016 01:02 #
Right now I am just adding or removing documents and executing queries. So, I imagine the time it takes to run CHECK is insignificant. But, if I am going to reuse code, then using the technique of always running CHECK before every operation seem like it might not be the best thing to do. Similarly assuming that my code elsewhere is correctly opening the database and setting a variable to say “isOpen” before it is needed also doesn’t seem like the best idea. But, I gather using CHECK all the time is the typical way to do this. So, I will do that. I thought of CHECK as create or open, it didn’t occur to me that it would optionally open.
Thanks,
Kendall
Quoted text · Christian Grün · 63 lines
On 6/19/16, 3:25 PM, "Christian Grün" <christian.gruen at gmail.com> wrote: >Hi Kendall, > >> Is using CHECK when the database is already open comparable in CPU cycles to testing a boolean variable that says open true or false? I understand that the time it takes for most commands to execute would make code logic usually insignificant. > >Check will definitely be slower than a pure boolean check. On the >other hand, I wouldn’t expect you to notice any delay, as the >execution time should only be a few milliseconds, or even less. > >What will be the processing time you require for this operation? There >may be some chance to improve the execution time of this command (it >just hasn’t been a bottleneck so far). > >Cheers, >Christian > > >> On 6/18/16, 4:54 AM, "Christian Grün" <christian.gruen at gmail.com> wrote: >> >>>Hi Kendall, >>> >>>I would indeed recommend you to use the CHECK command before doing any >>>other operations. This command does exactly what you seem to be >>>looking for (checking if the databases is opened; if not, open it). >>> >>>Does this help? >>>Christian >>> >>> >>> >>>On Wed, Jun 15, 2016 at 7:03 PM, Kendall Shaw <kendall.shaw at workday.com> wrote: >>>> Using ClientSession from java, I do this to check if the database is open: >>>> >>>> >>>> >>>> try { >>>> >>>> replace(somewhere, new ArrayInput(bytes)); >>>> >>>> } catch (IOException e) { >>>> String msg = e.getMessage(); >>>> >>>> if (msg == null || !msg.contains(“No database opened”)) >>>> >>>> throw e; >>>> >>>> execute(new Check(databaseName)); >>>> >>>> replace(somewhere, new ArrayInput(bytes)); >>>> >>>> } >>>> >>>> >>>> >>>> Is there a better way to detect if the database is open than testing the >>>> exception message text? >>>> >>>> >>>> >>>> Kendall >>>> >>>> >>
CG
Christian Grün20 Jun 2016 01:54 #
I had a look at the actual BaseX sources, and I saw that OPEN is extremely fast if your database is already opened (it boils down to a simple name equality test). Obviously you should only use it if the addressed database already exists.
If you want to have a single operation for opening and updating documents, XQuery (with
db:add and db:replace) is probably the best solution.Quoted text · Kendall Shaw · 1 line
Am 20.06.2016 1:03 vorm. schrieb "Kendall Shaw" <kendall.shaw at workday.com>:
Right now I am just adding or removing documents and executing queries. So, I imagine the time it takes to run CHECK is insignificant. But, if I am going to reuse code, then using the technique of always running CHECK before every operation seem like it might not be the best thing to do. Similarly assuming that my code elsewhere is correctly opening the database and setting a variable to say “isOpen” before it is needed also doesn’t seem like the best idea. But, I gather using CHECK all the time is the typical way to do this. So, I will do that. I thought of CHECK as create or open, it didn’t occur to me that it would optionally open.
Thanks,
Kendall
Quoted text · Christian Grün · 4 lines
On 6/19/16, 3:25 PM, "Christian Grün" <christian.gruen at gmail.com> wrote: >Hi Kendall, > >> Is using CHECK when the database is already open comparable in CPU
cycles to testing a boolean variable that says open true or false? I understand that the time it takes for most commands to execute would make code logic usually insignificant.
Quoted text · Kendall Shaw · 29 lines
> >Check will definitely be slower than a pure boolean check. On the >other hand, I wouldn’t expect you to notice any delay, as the >execution time should only be a few milliseconds, or even less. > >What will be the processing time you require for this operation? There >may be some chance to improve the execution time of this command (it >just hasn’t been a bottleneck so far). > >Cheers, >Christian > > >> On 6/18/16, 4:54 AM, "Christian Grün" <christian.gruen at gmail.com> wrote: >> >>>Hi Kendall, >>> >>>I would indeed recommend you to use the CHECK command before doing any >>>other operations. This command does exactly what you seem to be >>>looking for (checking if the databases is opened; if not, open it). >>> >>>Does this help? >>>Christian >>> >>> >>> >>>On Wed, Jun 15, 2016 at 7:03 PM, Kendall Shaw <kendall.shaw at workday.com> wrote: >>>> Using ClientSession from java, I do this to check if the database is
open:
Quoted text · Kendall Shaw · 32 lines
>>>> >>>> >>>> >>>> try { >>>> >>>> replace(somewhere, new ArrayInput(bytes)); >>>> >>>> } catch (IOException e) { >>>> String msg = e.getMessage(); >>>> >>>> if (msg == null || !msg.contains(“No database opened”)) >>>> >>>> throw e; >>>> >>>> execute(new Check(databaseName)); >>>> >>>> replace(somewhere, new ArrayInput(bytes)); >>>> >>>> } >>>> >>>> >>>> >>>> Is there a better way to detect if the database is open than testing the >>>> exception message text? >>>> >>>> >>>> >>>> Kendall >>>> >>>> >>
Written for BaseX 8.4. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery