Page 1 of 1

Help with Transactions

PostPosted:Mon Dec 03, 2012 6:44 pm
by noxious
Hello, I haven't got any experience in Transactions and I would appreciate it if someone could give me a hint:
I have noticed in the OpenKM source code that you implement transactions when working with repository or communicating with database using Hibernation.
Let's say that I have a method that does these actions:
-query the database using jdbc connection (not Hibernate)
-update the database using jdbc connection (not Hibernate)
-grant a user on a document using OKMAuth class
-revoke an other user from the same document using OKMAuth class
Can I include all these things inside a single global transaction so that If something goes wrong everything will rollback?
Is OKMAuth compatible with com.openkm.util.Transaction? Can i do something like this? :
Code: Select all
XASession session = (XASession) JCRUtils.getSession();
			Transaction t = new Transaction(session);
			t.start();
                        OKMAuth.getInstance().grantUser()...
                        OKMAuth.getInstance().revokeUser()...
                        t.end();
			t.commit();
                        
If not, is there a way I can make my jdbc connector and OKMAuth be handled from a global transaction manager using an XA interface or something like this?
Sorry for the sillyness of my question but I have only worked with local database Transactions. JTA is something new for me and I am trying to figure out how your transactions work :oops:

Re: Help with Transactions

PostPosted:Wed Dec 05, 2012 10:24 pm
by jllort
First your database should be transaction, for example if you're on mysql should be ensure you're on transactional engine like inodb ( not with myisam )
Second as any transaction database you open transaction and then commit, if you take a look in source code you can see how is doing it with hibernate.
If you want to doing the same with jdbc or sql I suggest you google for "mysql transaction example"

Actually we're not using older JCRUtils. You should take a look at AuthDao http://doxygen.openkm.com/6.2.x/d6/dbe/ ... d_a_o.html

Note: Here I suppose you have configured mysql, otherside you got default hypersonic database.

Re: Help with Transactions

PostPosted:Thu Dec 06, 2012 12:40 pm
by noxious
I am indeed using MySQL with innodb and I can do transactions for multiple MySQL queries.
My problem is that I have created a method that:
a. Connects the database and inserts a record
b. Grants a role at a document using OKMAuth.getInstance().grantRole(...);
c. Revokes a role from the document using OKMAuth.getInstance().revokeRole(...);
So, I would like to have a transaction ,like that:
transaction.begin();
try{
a. Connects the database and inserts a record
b. Grants a role at a document using OKMAuth.getInstance().grantRole(...);
c. Revokes a role from the document using OKMAuth.getInstance().revokeRole(...);
transaction.commit();
} catch (Exception e){
transaction.rollback;
}
So, if a or b or c throws an exception all a,b,c will rollback.
I understand AuthDAO but there you just use hibernate transactions.
I need a transaction manager that can control database queries and java methods like OKMAuth.grantRole inside the same block of code...
Thanks for the answer!

Re: Help with Transactions

PostPosted:Sat Dec 08, 2012 5:55 pm
by jllort
The api directly controls tractaction on their own methods. I will try explain better, the problem is that each method you use from api has begin / end transaction ( OKMAuth.getInstance().grantRole(...) -> has their own transaction into method ).

That mean what you want to doing it not possible at this level access of the API. Now should go in more deep control NodeBaseDAO.grantRolePermissions ( etc... ), and create your own unified method. Not seems good solution too. I not see and easy way to implement it.

Anyway is strange you get exception with OKMAuth.getInstance().grantRole or revoke ( very strange ). On which kind of logic you get exception here ?

Re: Help with Transactions

PostPosted:Sun Dec 09, 2012 10:43 am
by noxious
No, I don't get any exceptions, everything works fine.
I just thought that it would be very nice if I could do that and I asked your opinion.
By the way, there is a posibility me contributing in the greek translations in the next months when I will be having more free time.
Again, thank you for your answers.

Re: Help with Transactions

PostPosted:Mon Dec 10, 2012 8:27 am
by pavila
First of all, the provided sample code:
Code: Select all
XASession session = (XASession) JCRUtils.getSession();
Works with Jacrabbit (OpenKM 5.1.x) not are transactions related to Hibernate. As I can understand you want to group several OpenKM API calls into an atomic operation. This can't work because Hibernate transactions are used inside the API calls to ensure atomicity: open transaction, fo the SQL modifications and make commit or rollback.

I read sometime ago about global transactions which can be use as you need but never tried. If you find anything about them, please share with us.