embedded mode thread-safe?

Thursday, 6 October 2016 · 8 messages · last reply 9 Oct 2016
EK
Erdal Karaca6 Oct 2016 20:00 #
Hi all, Is the embedded mode thread-safe? I mean, once I have the Context object and opened a database, is it safe to access (read/write) it from multiple threads? Will the same transaction mechanism apply as if the server mode would be used? Thanks.
CG
Christian Grün6 Oct 2016 20:07 #
Quoted text · Erdal Karaca · 2 lines
> I mean, once I have the Context object and opened a database, is it safe to > access (read/write) it from multiple threads?
It depends on how you do it ;) If you call Command.execute() with the same Context instance, you should be safe.
EK
Erdal Karaca9 Oct 2016 16:20 #
Hi Christian, I have setup a stress test to access the same DB in parallel. I have only one call to the Context constructor and using that same Context instance for all executions. I still get this exception:
Caused by: java.nio.channels.OverlappingFileLockException
at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1108)
at org.basex.io.random.TableDiskAccess.lock(TableDiskAccess.java:139)
at org.basex.data.DiskData.startUpdate(DiskData.java:207)
at org.basex.query.up.ContextModifier.apply(ContextModifier.java:131)
at org.basex.query.up.Updates.apply(Updates.java:157)
at org.basex.query.QueryContext.iter(QueryContext.java:351)
at org.basex.query.QueryProcessor.value(QueryProcessor.java:101)
I am accessing the returned elements (of the queries) via a Value.iter() iterator. The queries returns a sequence of 100 elements. Each access to the DB has its own subtree. I.e. if "user 1" accesses the DB, then he only accesses the subtree assigned to him. This is the DB tree structure: MyDB + users/usr1.xml/container + users/usr2.xml/container + ... + users/usrN.xml/container Are iterators also thread-safe? If not, what can I use instead? (Standalone unit test will follow.) 2016-10-06 20:07 GMT+02:00 Christian Grün <christian.gruen at gmail.com>:
Quoted text · Christian Grün · 7 lines
> > I mean, once I have the Context object and opened a database, is it safe > to > > access (read/write) it from multiple threads? > > It depends on how you do it ;) If you call Command.execute() with the > same Context instance, you should be safe. >
CG
Christian Grün9 Oct 2016 16:36 #
You may need to call XQuery.execute, or call QueryProcessor.register (and, finally, unregister) before retrieving results; but maybe you should prepare an MCVE for us?
Quoted text · Erdal Karaca · 47 lines
On Sun, Oct 9, 2016 at 4:20 PM, Erdal Karaca <erdal.karaca.de at gmail.com> wrote: > Hi Christian, > I have setup a stress test to access the same DB in parallel. I have only > one call to the Context constructor and using that same Context instance for > all executions. > I still get this exception: > Caused by: java.nio.channels.OverlappingFileLockException > at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255) > at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152) > at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1108) > at org.basex.io.random.TableDiskAccess.lock(TableDiskAccess.java:139) > at org.basex.data.DiskData.startUpdate(DiskData.java:207) > at org.basex.query.up.ContextModifier.apply(ContextModifier.java:131) > at org.basex.query.up.Updates.apply(Updates.java:157) > at org.basex.query.QueryContext.iter(QueryContext.java:351) > at org.basex.query.QueryProcessor.value(QueryProcessor.java:101) > > I am accessing the returned elements (of the queries) via a Value.iter() > iterator. > The queries returns a sequence of 100 elements. > > Each access to the DB has its own subtree. > I.e. if "user 1" accesses the DB, then he only accesses the subtree assigned > to him. > > This is the DB tree structure: > MyDB > + users/usr1.xml/container > + users/usr2.xml/container > + ... > + users/usrN.xml/container > > Are iterators also thread-safe? > If not, what can I use instead? > > (Standalone unit test will follow.) > > 2016-10-06 20:07 GMT+02:00 Christian Grün <christian.gruen at gmail.com>: >> >> > I mean, once I have the Context object and opened a database, is it safe >> > to >> > access (read/write) it from multiple threads? >> >> It depends on how you do it ;) If you call Command.execute() with the >> same Context instance, you should be safe. > >
EK
Erdal Karaca9 Oct 2016 16:44 #
Hi Christian, I have prepared a MCVE (see attachment). Not sure how to use QueryProcess.register/unregister. Is there an example? XQuery.execute() does not return objects, just a string? 2016-10-09 16:36 GMT+02:00 Christian Grün <christian.gruen at gmail.com>:
Quoted text · Christian Grün · 58 lines
> You may need to call XQuery.execute, or call QueryProcessor.register > (and, finally, unregister) before retrieving results; but maybe you > should prepare an MCVE for us? > > > > On Sun, Oct 9, 2016 at 4:20 PM, Erdal Karaca <erdal.karaca.de at gmail.com> > wrote: > > Hi Christian, > > I have setup a stress test to access the same DB in parallel. I have only > > one call to the Context constructor and using that same Context instance > for > > all executions. > > I still get this exception: > > Caused by: java.nio.channels.OverlappingFileLockException > > at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255) > > at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152) > > at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1108) > > at org.basex.io.random.TableDiskAccess.lock(TableDiskAccess.java:139) > > at org.basex.data.DiskData.startUpdate(DiskData.java:207) > > at org.basex.query.up.ContextModifier.apply(ContextModifier.java:131) > > at org.basex.query.up.Updates.apply(Updates.java:157) > > at org.basex.query.QueryContext.iter(QueryContext.java:351) > > at org.basex.query.QueryProcessor.value(QueryProcessor.java:101) > > > > I am accessing the returned elements (of the queries) via a Value.iter() > > iterator. > > The queries returns a sequence of 100 elements. > > > > Each access to the DB has its own subtree. > > I.e. if "user 1" accesses the DB, then he only accesses the subtree > assigned > > to him. > > > > This is the DB tree structure: > > MyDB > > + users/usr1.xml/container > > + users/usr2.xml/container > > + ... > > + users/usrN.xml/container > > > > Are iterators also thread-safe? > > If not, what can I use instead? > > > > (Standalone unit test will follow.) > > > > 2016-10-06 20:07 GMT+02:00 Christian Grün <christian.gruen at gmail.com>: > >> > >> > I mean, once I have the Context object and opened a database, is it > safe > >> > to > >> > access (read/write) it from multiple threads? > >> > >> It depends on how you do it ;) If you call Command.execute() with the > >> same Context instance, you should be safe. > > > > >
CG
Christian Grün9 Oct 2016 17:24 #
Would you mind rewriting it for Java 1.7? Thanks in advance, Christian
Quoted text · Erdal Karaca · 69 lines
On Sun, Oct 9, 2016 at 4:44 PM, Erdal Karaca <erdal.karaca.de at gmail.com> wrote: > Hi Christian, > I have prepared a MCVE (see attachment). > Not sure how to use QueryProcess.register/unregister. > Is there an example? > XQuery.execute() does not return objects, just a string? > > 2016-10-09 16:36 GMT+02:00 Christian Grün <christian.gruen at gmail.com>: >> >> You may need to call XQuery.execute, or call QueryProcessor.register >> (and, finally, unregister) before retrieving results; but maybe you >> should prepare an MCVE for us? >> >> >> >> On Sun, Oct 9, 2016 at 4:20 PM, Erdal Karaca <erdal.karaca.de at gmail.com> >> wrote: >> > Hi Christian, >> > I have setup a stress test to access the same DB in parallel. I have >> > only >> > one call to the Context constructor and using that same Context instance >> > for >> > all executions. >> > I still get this exception: >> > Caused by: java.nio.channels.OverlappingFileLockException >> > at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255) >> > at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152) >> > at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1108) >> > at org.basex.io.random.TableDiskAccess.lock(TableDiskAccess.java:139) >> > at org.basex.data.DiskData.startUpdate(DiskData.java:207) >> > at org.basex.query.up.ContextModifier.apply(ContextModifier.java:131) >> > at org.basex.query.up.Updates.apply(Updates.java:157) >> > at org.basex.query.QueryContext.iter(QueryContext.java:351) >> > at org.basex.query.QueryProcessor.value(QueryProcessor.java:101) >> > >> > I am accessing the returned elements (of the queries) via a Value.iter() >> > iterator. >> > The queries returns a sequence of 100 elements. >> > >> > Each access to the DB has its own subtree. >> > I.e. if "user 1" accesses the DB, then he only accesses the subtree >> > assigned >> > to him. >> > >> > This is the DB tree structure: >> > MyDB >> > + users/usr1.xml/container >> > + users/usr2.xml/container >> > + ... >> > + users/usrN.xml/container >> > >> > Are iterators also thread-safe? >> > If not, what can I use instead? >> > >> > (Standalone unit test will follow.) >> > >> > 2016-10-06 20:07 GMT+02:00 Christian Grün <christian.gruen at gmail.com>: >> >> >> >> > I mean, once I have the Context object and opened a database, is it >> >> > safe >> >> > to >> >> > access (read/write) it from multiple threads? >> >> >> >> It depends on how you do it ;) If you call Command.execute() with the >> >> same Context instance, you should be safe. >> > >> > > >
EK
Erdal Karaca9 Oct 2016 19:11 #
I have re-written the test to use 1.7, and also adapted register/unregister, but still I get this exception:
Caused by: org.basex.core.BaseXException: Database 'stress-test' is being
updated, or update was not completed.
at org.basex.data.DiskData.startUpdate(DiskData.java:210)
at org.basex.query.up.ContextModifier.apply(ContextModifier.java:131)
2016-10-09 17:24 GMT+02:00 Christian Grün <christian.gruen at gmail.com>:
Quoted text · Christian Grün · 81 lines
> Would you mind rewriting it for Java 1.7? > > Thanks in advance, > Christian > > > On Sun, Oct 9, 2016 at 4:44 PM, Erdal Karaca <erdal.karaca.de at gmail.com> > wrote: > > Hi Christian, > > I have prepared a MCVE (see attachment). > > Not sure how to use QueryProcess.register/unregister. > > Is there an example? > > XQuery.execute() does not return objects, just a string? > > > > 2016-10-09 16:36 GMT+02:00 Christian Grün <christian.gruen at gmail.com>: > >> > >> You may need to call XQuery.execute, or call QueryProcessor.register > >> (and, finally, unregister) before retrieving results; but maybe you > >> should prepare an MCVE for us? > >> > >> > >> > >> On Sun, Oct 9, 2016 at 4:20 PM, Erdal Karaca <erdal.karaca.de at gmail.com > > > >> wrote: > >> > Hi Christian, > >> > I have setup a stress test to access the same DB in parallel. I have > >> > only > >> > one call to the Context constructor and using that same Context > instance > >> > for > >> > all executions. > >> > I still get this exception: > >> > Caused by: java.nio.channels.OverlappingFileLockException > >> > at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255) > >> > at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152) > >> > at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1108) > >> > at org.basex.io.random.TableDiskAccess.lock(TableDiskAccess.java:139) > >> > at org.basex.data.DiskData.startUpdate(DiskData.java:207) > >> > at org.basex.query.up.ContextModifier.apply(ContextModifier.java:131) > >> > at org.basex.query.up.Updates.apply(Updates.java:157) > >> > at org.basex.query.QueryContext.iter(QueryContext.java:351) > >> > at org.basex.query.QueryProcessor.value(QueryProcessor.java:101) > >> > > >> > I am accessing the returned elements (of the queries) via a > Value.iter() > >> > iterator. > >> > The queries returns a sequence of 100 elements. > >> > > >> > Each access to the DB has its own subtree. > >> > I.e. if "user 1" accesses the DB, then he only accesses the subtree > >> > assigned > >> > to him. > >> > > >> > This is the DB tree structure: > >> > MyDB > >> > + users/usr1.xml/container > >> > + users/usr2.xml/container > >> > + ... > >> > + users/usrN.xml/container > >> > > >> > Are iterators also thread-safe? > >> > If not, what can I use instead? > >> > > >> > (Standalone unit test will follow.) > >> > > >> > 2016-10-06 20:07 GMT+02:00 Christian Grün <christian.gruen at gmail.com > >: > >> >> > >> >> > I mean, once I have the Context object and opened a database, is it > >> >> > safe > >> >> > to > >> >> > access (read/write) it from multiple threads? > >> >> > >> >> It depends on how you do it ;) If you call Command.execute() with the > >> >> same Context instance, you should be safe. > >> > > >> > > > > > >
CG
Christian Grün9 Oct 2016 20:09 #
Thanks. It works if you call register/unregister for all QueryProcessor instances (including the nested one).
Quoted text · Erdal Karaca · 95 lines
On Sun, Oct 9, 2016 at 7:11 PM, Erdal Karaca <erdal.karaca.de at gmail.com> wrote: > I have re-written the test to use 1.7, and also adapted register/unregister, > but still I get this exception: > > Caused by: org.basex.core.BaseXException: Database 'stress-test' is being > updated, or update was not completed. > at org.basex.data.DiskData.startUpdate(DiskData.java:210) > at org.basex.query.up.ContextModifier.apply(ContextModifier.java:131) > > 2016-10-09 17:24 GMT+02:00 Christian Grün <christian.gruen at gmail.com>: >> >> Would you mind rewriting it for Java 1.7? >> >> Thanks in advance, >> Christian >> >> >> On Sun, Oct 9, 2016 at 4:44 PM, Erdal Karaca <erdal.karaca.de at gmail.com> >> wrote: >> > Hi Christian, >> > I have prepared a MCVE (see attachment). >> > Not sure how to use QueryProcess.register/unregister. >> > Is there an example? >> > XQuery.execute() does not return objects, just a string? >> > >> > 2016-10-09 16:36 GMT+02:00 Christian Grün <christian.gruen at gmail.com>: >> >> >> >> You may need to call XQuery.execute, or call QueryProcessor.register >> >> (and, finally, unregister) before retrieving results; but maybe you >> >> should prepare an MCVE for us? >> >> >> >> >> >> >> >> On Sun, Oct 9, 2016 at 4:20 PM, Erdal Karaca >> >> <erdal.karaca.de at gmail.com> >> >> wrote: >> >> > Hi Christian, >> >> > I have setup a stress test to access the same DB in parallel. I have >> >> > only >> >> > one call to the Context constructor and using that same Context >> >> > instance >> >> > for >> >> > all executions. >> >> > I still get this exception: >> >> > Caused by: java.nio.channels.OverlappingFileLockException >> >> > at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255) >> >> > at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152) >> >> > at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1108) >> >> > at org.basex.io.random.TableDiskAccess.lock(TableDiskAccess.java:139) >> >> > at org.basex.data.DiskData.startUpdate(DiskData.java:207) >> >> > at org.basex.query.up.ContextModifier.apply(ContextModifier.java:131) >> >> > at org.basex.query.up.Updates.apply(Updates.java:157) >> >> > at org.basex.query.QueryContext.iter(QueryContext.java:351) >> >> > at org.basex.query.QueryProcessor.value(QueryProcessor.java:101) >> >> > >> >> > I am accessing the returned elements (of the queries) via a >> >> > Value.iter() >> >> > iterator. >> >> > The queries returns a sequence of 100 elements. >> >> > >> >> > Each access to the DB has its own subtree. >> >> > I.e. if "user 1" accesses the DB, then he only accesses the subtree >> >> > assigned >> >> > to him. >> >> > >> >> > This is the DB tree structure: >> >> > MyDB >> >> > + users/usr1.xml/container >> >> > + users/usr2.xml/container >> >> > + ... >> >> > + users/usrN.xml/container >> >> > >> >> > Are iterators also thread-safe? >> >> > If not, what can I use instead? >> >> > >> >> > (Standalone unit test will follow.) >> >> > >> >> > 2016-10-06 20:07 GMT+02:00 Christian Grün >> >> > <christian.gruen at gmail.com>: >> >> >> >> >> >> > I mean, once I have the Context object and opened a database, is >> >> >> > it >> >> >> > safe >> >> >> > to >> >> >> > access (read/write) it from multiple threads? >> >> >> >> >> >> It depends on how you do it ;) If you call Command.execute() with >> >> >> the >> >> >> same Context instance, you should be safe. >> >> > >> >> > >> > >> > > >

⚡Generated with XQuery