<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Multi-threading and transactions in Alfresco Archive</title>
    <link>https://connect.hyland.com/t5/alfresco-archive/multi-threading-and-transactions/m-p/179378#M132508</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Within a repository action, we need to run some CPU-eating code that would benefit well from multi-threading. The main problem is that this code is accessing content from Alfresco (read and write), which means it needs authentication and an active transaction.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried several approaches already with no success so far. I think we get two options here:&lt;/SPAN&gt;&lt;BR /&gt;&lt;UL&gt;&lt;LI&gt;Start threads within the current transaction and ensure that they are able to re-use the current transaction. I would assume that transaction information is stored in some ThreadLocal facility, which means that using a common ThreadGroup could be a way to let the child threads re-use the transaction.&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Pass a current transaction reference to the child threads and then start a new nested transaction in each thread.&lt;/LI&gt;&lt;/UL&gt;&lt;SPAN&gt;In each case, the main objective is to let all work from all threads be commited or rolled back at once.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To illustrate my point, here is small piece of code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="language-none line-numbers"&gt;&lt;CODE&gt;&lt;BR /&gt;final NodeRef nodeRef = …&lt;BR /&gt;&lt;BR /&gt;newTransaction();&lt;BR /&gt;&lt;BR /&gt;final Thread thread = new Thread(new Runnable()&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void run()&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nodeService.addAspect(nodeRef, ContentModel.ASPECT_CLASSIFIABLE, null);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;});&lt;BR /&gt;&lt;BR /&gt;thread.start();&lt;BR /&gt;thread.join();&lt;BR /&gt;&lt;BR /&gt;commit();&lt;BR /&gt;&lt;BR /&gt;newTransaction();&lt;BR /&gt;assert nodeService.hasAspect(nodeRef, ContentModel.ASPECT_CLASSIFIABLE);&lt;BR /&gt;commit();&lt;BR /&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;SPAN&gt;As you may have guessed already, newTransaction() and commit() are fictional methods.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The assertion fails. There is also some error in the console (uncaught exception obviously) regarding the missing active transaction that occurs during the run() method invocation from the child thread.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, is there a way to run multiple threads within a transaction so that I can have the 3 other lazy CPU cores compute something for a change? &lt;img id="smileywink" class="emoticon emoticon-smileywink" src="https://connect.hyland.com/i/smilies/16x16_smiley-wink.png" alt="Smiley Wink" title="Smiley Wink" /&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 23 Sep 2008 16:39:36 GMT</pubDate>
    <dc:creator>herve_quiroz</dc:creator>
    <dc:date>2008-09-23T16:39:36Z</dc:date>
    <item>
      <title>Multi-threading and transactions</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/multi-threading-and-transactions/m-p/179378#M132508</link>
      <description>Within a repository action, we need to run some CPU-eating code that would benefit well from multi-threading. The main problem is that this code is accessing content from Alfresco (read and write), which means it needs authentication and an active transaction.I tried several approaches already with</description>
      <pubDate>Tue, 23 Sep 2008 16:39:36 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/multi-threading-and-transactions/m-p/179378#M132508</guid>
      <dc:creator>herve_quiroz</dc:creator>
      <dc:date>2008-09-23T16:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: Multi-threading and transactions</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/multi-threading-and-transactions/m-p/179379#M132509</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I don't believe a single Alfresco transaction can be used in more than a single thread, in large part because Hibernate (the ORM tool Alfresco uses internally) doesn't support this mode of operation (Hibernate sessions do not support multi-threading - each thread must have its own Hibernate session).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Peter&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Sep 2008 18:02:49 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/multi-threading-and-transactions/m-p/179379#M132509</guid>
      <dc:creator>pmonks</dc:creator>
      <dc:date>2008-09-23T18:02:49Z</dc:date>
    </item>
    <item>
      <title>Re: Multi-threading and transactions</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/multi-threading-and-transactions/m-p/179380#M132510</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I was more or less expecting such bad news… Since my post, I have read even more pages on the subject (hibernate vs threads).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for your help.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Sep 2008 20:53:44 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/multi-threading-and-transactions/m-p/179380#M132510</guid>
      <dc:creator>herve_quiroz</dc:creator>
      <dc:date>2008-09-23T20:53:44Z</dc:date>
    </item>
  </channel>
</rss>

