I recently wrote some code to try to handle a similar situation. In my
case I knew I needed a transaction to be active at various points so I
created a TransactionHolder. I create the holder and passing the object
that has implements Transactional as well as the type of ReadWrite I want.
If the transaction is active it does nothing (and I hope the proper
transaction has been started) otherwise It starts the transaction.
Ad the end I call commit or abort as appropriate. If I did not start the
transaction the commit, abort or end is ignored.
I think there may be an issue with abort in that it should probablyset up
end() to throw an exception when I have not created the transaction so
that the outer transaction will fail.
import org.apache.jena.query.ReadWrite;
import org.apache.jena.sparql.core.Transactional;
public class TransactionHolder {
private final Transactional txn;
private final boolean started;
private final ReadWrite rw;
public TransactionHolder( Transactional txn, ReadWrite rw )
{
this.txn = txn;
this.rw = rw;
started = ! txn.isInTransaction();
if (started)
{
txn.begin( rw );
}
}
public boolean ownsTranaction() {
return started;
}
public void commit() {
if (started) {
txn.commit();
}
}
public void abort() {
if (started)
{
txn.abort();
}
}
public void end() {
if (started) {
txn.end();
}
}
}
On Wed, Dec 27, 2017 at 11:03 AM, dandh988 <[email protected]> wrote:
> You cannot nest transactions nor can you promote a read to a write.
> You need to rewrite your code or use txn which correctly checks if a
> transaction is available and if not will begin the correct one, either READ
> or WRITE.
>
>
> Dick
> -------- Original message --------From: George News <[email protected]>
> Date: 27/12/2017 10:27 (GMT+00:00) To: Jena User Mailing List <
> [email protected]> Subject: Txn code not handling type of transaction
> Hi,
>
> As you know from other threads I'm having some issues with transactions.
> Your suggestion is to use Txn instead of begin/end. Just for curiosity I
> have checked the Txn code at [1] and it seems that inside you use
> begin/end.
>
> However I have a doubt concerning how you handle the begin/end for READ
> and WRITE. It seems that you open a transaction based on
> txn.isInTransaction(), but how do you know if it is a READ or WRITE?
>
> If you create something like:
>
> Txn.executeRead(dataset, {
> Txn.executeWrite(dataset, {
> // Whatever
> }
> }
> }
>
> the txn.begin(ReadWrite.WRITE) is not called and therefore it might be
> leading to unexepected behaviours for the txn.commit().
>
> could you give some hints on how this is handle internally? Before fully
> modify the code I have, it might be easier to replicate the txn
> behaviour ;) but I would like to know the above (if possible).
>
> As always, thanks in advanced
> Jorge
>
> [1]: jena/jena-arq/src/main/java/org/apache/jena/system/Txn.java
>
--
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren