{"id":1501,"date":"2014-01-10T11:53:50","date_gmt":"2014-01-10T16:53:50","guid":{"rendered":"http:\/\/alejandroayala.solmedia.ec\/?p=1501"},"modified":"2014-01-10T11:58:27","modified_gmt":"2014-01-10T16:58:27","slug":"tipos-de-transacciones-jpa-y-jta","status":"publish","type":"post","link":"https:\/\/alejandroayala.solmedia.ec\/?p=1501","title":{"rendered":"Tipos de transacciones JPA y JTA"},"content":{"rendered":"<p style=\"text-align: justify;\">En el archivo de configuraci\u00f3n de <em>JPA<\/em> <strong><em>persistence.xml<\/em><\/strong> se especifica el tipo de transacci\u00f3n(<em>transaction-type<\/em>) para cada unidad de persistencia.<\/p>\n<p>[xml]<br \/>\n&lt;persistence &#8230; &gt;<br \/>\n&lt;persistence-unit name=&quot;&#8230;&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt;<br \/>\n&#8230;<br \/>\n&lt;\/persistence-unit&gt;<br \/>\n&#8230;<br \/>\n&lt;\/persistence&gt;<br \/>\n[\/xml]<\/p>\n<p><!--more--><\/p>\n<p>Hay dos posibles valores:<em><strong> RESOURCE_LOCAL y JTA.<\/strong><\/em><\/p>\n<h2>Resource Local<\/h2>\n<ul>\n<li style=\"text-align: justify;\">Usado para aplicaciones <em>standalone<\/em>.<\/li>\n<li style=\"text-align: justify;\">Es el responsable de la creaci\u00f3n del <em>EntityManager<\/em> mediante <em>EntityManagerFactory<\/em><\/li>\n<li style=\"text-align: justify;\">Las transacciones son de tipo <em>EntityTransaction<\/em><\/li>\n<li style=\"text-align: justify;\">Se debe usar <em><strong>begin()<\/strong><\/em> y <em><strong>commit()<\/strong><\/em> en cada transacci\u00f3n<\/li>\n<li style=\"text-align: justify;\">Si m\u00faltiples instancias de <em>EntityManager<\/em> son creadas para la misma unidad de persistencia, <strong>m\u00faltiples contextos de persistencia son creados tambi\u00e9n<\/strong>. Esto quiere decir que las transacciones NO est\u00e1n sincronizadas en toda la aplicaci\u00f3n. Una soluci\u00f3n a esto es usar <em>UserTransaction<\/em>. Otra es evitar el uso de m\u00faltiples <em>EntityManager<\/em> para la misma unidad de persistencia.<\/li>\n<\/ul>\n<h3>Uso<\/h3>\n<p>[java]<br \/>\n\/\/ Creating resources<br \/>\nEntityManagerFactory EMF<br \/>\n= Persistence.createEntityManagerFactory(&quot;Standalone&quot;);<br \/>\nEntityManager EM = EMF.createEntityManager();<\/p>\n<p>\/\/ Transaction<br \/>\nEntityTransaction et = EM.getTransaction();<\/p>\n<p>try {<br \/>\net.begin();<br \/>\n\/\/ Operations&#8230;<br \/>\net.commit();<br \/>\n} catch(Exception ex) {<br \/>\net.rollback();<br \/>\n}<\/p>\n<p>\/\/ Closing resources<br \/>\nEM.close();<br \/>\nEMF.close();<br \/>\n[\/java]<\/p>\n<h2>JTA<\/h2>\n<ul>\n<li style=\"text-align: justify;\"><em>JTA\u00a0<\/em> significa <strong><em>Java Transaction API<\/em><\/strong>. Es parte de la especificaci\u00f3n <em>Java EE<\/em>.<\/li>\n<li style=\"text-align: justify;\">Es el t\u00edpico valor usado para el contenedor de la aplicaci\u00f3n<\/li>\n<li style=\"text-align: justify;\">El <em>EntityManager<\/em> es provisto por el contenedor de la aplicaci\u00f3n(<strong>una \u00fanica instancia para toda la aplicaci\u00f3n<\/strong>)<\/li>\n<li style=\"text-align: justify;\">Las transacciones se pueden declara con <em>@TransactionAttribute<\/em> y estas ser\u00e1n gestionadas autom\u00e1ticamente.<\/li>\n<li style=\"text-align: justify;\">Todas las transacciones est\u00e1n sincronizadas en toda la aplicaci\u00f3n con una \u00fanica transacci\u00f3n <em>JTA<\/em>.<\/li>\n<\/ul>\n<h3>Uso:<\/h3>\n<p>[java]<br \/>\n@PersistenceContext(unitName=&quot;Container&quot;)<br \/>\nprivate EntityManager EM;<\/p>\n<p>@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)<br \/>\npublic void myMethod() throws Exception {<\/p>\n<p>Item i = EM.find(Item.class, 997);<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p style=\"text-align: justify;\">Mas acerca de transacciones <em>JPA\/JTA<\/em> <a href=\"http:\/\/en.wikibooks.org\/wiki\/Java_Persistence\/Transactions\" target=\"_blank\">aqu\u00ed<\/a><\/p>\n<p style=\"text-align: justify;\">Excelente informaci\u00f3n sobre Transacciones <a href=\"http:\/\/www.ibm.com\/developerworks\/java\/library\/j-ts1\/index.html\" target=\"_blank\">ac\u00e1<\/a><\/p>\n<p style=\"text-align: justify;\">Fuente:\u00a0 <a href=\"http:\/\/www.mytechnotes.biz\/2012\/08\/jpa-transaction-types-and-jta.html\" target=\"_blank\">http:\/\/www.mytechnotes.biz\/2012\/08\/jpa-transaction-types-and-jta.html<\/a><\/p>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=a1ejo_ayala\" class=\"twitter-share-button\" data-size=\"large\">Twittear<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>En el archivo de configuraci\u00f3n de JPA persistence.xml se especifica el tipo de transacci\u00f3n(transaction-type) para cada unidad de persistencia. [xml] &lt;persistence &#8230; &gt; &lt;persistence-unit name=&quot;&#8230;&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt; &#8230; &lt;\/persistence-unit&gt; &#8230; &lt;\/persistence&gt; [\/xml]<\/p>\n","protected":false},"author":1,"featured_media":690,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[506,203,420,421,422,423,174,424],"_links":{"self":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts\/1501"}],"collection":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1501"}],"version-history":[{"count":5,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts\/1501\/revisions"}],"predecessor-version":[{"id":1506,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts\/1501\/revisions\/1506"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/media\/690"}],"wp:attachment":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}