I was going to open the local session and keep it open for the lifetime of the webapp like I would do with a JDBC session object. That would not allow me to use the autoclose. Is it possible or recommended to keep the one session object like that?
Absolutely. BaseX sessions are pretty light-weight, but if you want to repeatedly access a database/collection (as you did), and avoid reopening it every time, it can make sense to keep sessions open.
On March 11, 2017 11:07:31 AM EST, "Christian Grün" christian.gruen@gmail.com wrote:
Dear Aaron (and thanks Kendall),
A little addendum: If you use Java’s try-with-resources statement…
try (LocalSession session = new LocalSession(this.context))
…there will be no need to manually close your connection.
Best, Christian
On Fri, Mar 10, 2017 at 10:47 PM, AJ Weber aweber@comcast.net wrote:
OK, changing this so that the code flows like yours works much, much better. I now return all 3 documents I expect are in the database.
FOLLOW-UP Question: How do I close the database properly with these classes? I had been doing a context.closeDB() when shutting-down. Do I just do a localSession.close() now? Or do I need to do both?
Thanks again!!!
On 3/10/2017 4:27 PM, AJ Weber wrote:
Well, one difference I spot is that I am opening (via the snippet I sent before) the database BEFORE creating a LocalSession and I am using the new Check(...).execute(context);
The "context" is kept in the object's class-level.
Your code creates the context, associates the new LocalSession with the context, and THEN opens the existing database. (That's more akin to how a JDBC session would go...but I did not see any java examples with this, I followed the examples on the java page and only found LocalSession via a google search.)
There doesn't appear to be any details in the javadoc or anything I can find regarding how to use LocalSession, so I could be off base from that pov.
-AJ
On 3/10/2017 4:18 PM, Kendall Shaw wrote:
This worked for me, where “test-local” contains a document:
Context context = new Context(); LocalSession s = new LocalSession(context); s.execute(new Check("test-local")); try (Query q = s.query("for $doc in collection() return $doc"))
{
while (q.more()) { System.out.println(q.next()); } }
But, why your code isn’t working could be useful to know. It seems like there are some details left out.
Kendall
From: basex-talk-bounces@mailman.uni-konstanz.de on behalf of AJ Weber aweber@comcast.net Date: Friday, March 10, 2017 at 11:40 AM To: "basex-talk@mailman.uni-konstanz.de" basex-talk@mailman.uni-konstanz.de Subject: Re: [basex-talk] Simple query
On 3/10/2017 2:24 PM, Kendall Shaw wrote:
Michael Seiferle gave the answer already, I think. What would collection() be referring to? How would BaseX know what to return? Probably there is no database specified. Presumably, in the GUI you have opened a database.
Yes, I did not include my code to open the database. There is a database open and this does not throw an exception, it just returns no results.
In my constructor of this class there is the following code: new Check(this.MyID).execute(context);
(MyID is a String identifying the database I want to work with exclusively with this context passed.)
As I mentioned in a separate post, my query where matches(uri...) works fine...every time. That query also does not have a specific collection in the query.
Kendall
From: basex-talk-bounces@mailman.uni-konstanz.de on behalf of AJ Weber aweber@comcast.net Date: Friday, March 10, 2017 at 6:44 AM To: Fabrice ETANCHAUD fetanchaud@groupefbo.com, "basex-talk@mailman.uni-konstanz.de" basex-talk@mailman.uni-konstanz.de Subject: Re: [basex-talk] Simple query
On 3/9/2017 3:46 AM, Fabrice ETANCHAUD wrote:
Hello Aaron,
You would learn faster by using the BaseXGUI application,
You will benefit from syntax highlighting, real time execution, and hits on function signatures.
I am trying the GUI now. It is an excellent tool!
However, issuing the same exact XQuery in the GUI returns 3 documents (which is what I would have originally expected).
Maybe it is an issue with how I setup my query in java? Here is my code:
try (LocalSession session = new LocalSession(this.context)) { //test try (Query q = session.query("for $doc in collection()
return $doc")) { while (q.more()) { LOG.debug("RESULT: " + q.next()); } } } catch (Exception e) { LOG.error("Could not execute query " + statement, e); }
Best regards,
Fabrice
De : basex-talk-bounces@mailman.uni-konstanz.de [mailto:basex-talk-bounces@mailman.uni-konstanz.de] De la part de Aaron Weber Envoyé : jeudi 9 mars 2017 00:31 À : basex-talk@mailman.uni-konstanz.de Objet : [basex-talk] Simple query
Newbie alert.
I'm trying to get my feet wet with BaseX, and in doing so, am trying to understand XQuery and how to apply it to a database full of documents (not just a single document that is typically queried).
I am using Java and can post my code, but with a LocalSession, and a query, the following produces 0 results.
For $doc in collection() return $doc
I realize there's no "where", and in the sql world that would match all. Maybe not in XQuery?
Obviously just a test query, but I need to start somewhere. :-)
Thanks for any help!
AJ