Return-Path: X-Original-To: apmail-deltaspike-users-archive@www.apache.org Delivered-To: apmail-deltaspike-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5823318C25 for ; Wed, 2 Sep 2015 21:54:43 +0000 (UTC) Received: (qmail 89138 invoked by uid 500); 2 Sep 2015 21:54:43 -0000 Delivered-To: apmail-deltaspike-users-archive@deltaspike.apache.org Received: (qmail 89037 invoked by uid 500); 2 Sep 2015 21:54:42 -0000 Mailing-List: contact users-help@deltaspike.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@deltaspike.apache.org Delivered-To: mailing list users@deltaspike.apache.org Received: (qmail 89020 invoked by uid 99); 2 Sep 2015 21:54:42 -0000 Received: from Unknown (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Sep 2015 21:54:42 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 349FEC0F96 for ; Wed, 2 Sep 2015 21:54:42 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 2.879 X-Spam-Level: ** X-Spam-Status: No, score=2.879 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HTML_MESSAGE=3, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd4-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-eu-west.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id mSSS-Ts1R3y8 for ; Wed, 2 Sep 2015 21:54:32 +0000 (UTC) Received: from mail-ig0-f177.google.com (mail-ig0-f177.google.com [209.85.213.177]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with ESMTPS id E90BB21381 for ; Wed, 2 Sep 2015 21:54:31 +0000 (UTC) Received: by igbkq10 with SMTP id kq10so650481igb.0 for ; Wed, 02 Sep 2015 14:54:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=SqONqryorIytTCY/jdhq38LW07RkZ6JjhXeFWz84a8c=; b=OA/7E51NAyauL7NPpYcKYTwlfnG1C+nFT0CpSz8lLj4hOEF+Y1Ffv2PBviJ8c9eNmM q5N61H7BGhdZJCSow6lpPaqdkqLvxhf6FT4LafBw5HFZMPJ8gr8FI3r2OHXj6i083uUp PEWk7SV29Y/UoS6MWWEWUEXAxiBNd8/JdhR1NYKIWf87pd95fqWD3G/Ekh/iTTBR6eEI qfzNL3j8U8AoQmU/BA7UKtoTMkB44aaW8rXOQZR1CSCZgF+2ei4ZKbcXGKALfl+SymIu pMYlrGdXKrkEwJXylx7qaA02N2Kz38yinx04O9MyPe0QFyTkgiSjq0OyLlLQKuSKCJYY C2aQ== MIME-Version: 1.0 X-Received: by 10.50.122.10 with SMTP id lo10mr7133052igb.76.1441230870719; Wed, 02 Sep 2015 14:54:30 -0700 (PDT) Received: by 10.36.200.135 with HTTP; Wed, 2 Sep 2015 14:54:30 -0700 (PDT) Date: Wed, 2 Sep 2015 14:54:30 -0700 Message-ID: Subject: UserTransactionResolver in Tomcat From: David Smalley To: users@deltaspike.apache.org Content-Type: multipart/alternative; boundary=089e0153836216bd70051ecab6d2 --089e0153836216bd70051ecab6d2 Content-Type: text/plain; charset=UTF-8 Hello, I am using the Deltaspike JPA module + WELD in a web app targeting Tomcat (8). I have set up Atomikos Transaction Essentials to provide JTA transactions, and all of that is working fine. I want to use Deltaspike for the @Transactional and @TransactionScoped annotations. After enabling the BeanManagedTransactionStrategy, I received NameNotFound exceptions for TransactionSynchronizationRegistry. Some quick research showed that Atomikos implements JTA 1.0 which doesn't include the registry, and initially I thought this was the problem, but actually, it isn't. UserTransactionResolver declares a field @Resource UserTransaction userTransaction. The code seems to assume the only thing which can go wrong with this is that injection fails and it might be null, but actually, in Tomcat, it throws a NameNotFound JNDI exception when it is dynamically instantiated in resolveUserTransaction(), and so its alternative lookup logic is never called. resolveUserTransaction() catches this exception and always returns null, which is interpreted as there being an active CMT, which causes the lookup for the none-existent registry, but that is already wrong...there is no EJB and thus no CMT in a plain Tomcat environment. I have been able to make this work by changing the code in one of two ways: either change the annotation to @Resource(mappedName = "java:comp/UserTransaction") which causes the injection to succeed, or remove it entirely, in which case the field is null, and the alternative lookup logic is (successfully) invoked. After either of these changes, the BeanManagedUserTransactionStrategy works correctly, and I have declarative JTA transactions with plain Tomcat + Atomikos + Eclipselink. However, I don't want to build my own version of Deltaspike for deployment. I have worked around this problem by providing my own transaction strategy which extends BeanManagedUserTransactionStrategy and overrides resolveUserTransaction() like this: public UserTransaction resolveUserTransaction() { UserTransaction userTransaction = super().resolveUserTransaction(); if (userTransaction == null) { userTransaction = JNDIUtils.lookup("java:comp/UserTransaction", UserTransaction.class); } return userTransaction; } It works, but it feels like a kludge. For what it is worth, the spec recommends against assigning to injected fields, probably because you usually get a proxy rather than a plain object. I think the use of @Resource in UserTransactionResolver is dangerous, since it can throw an exception during instantiation, which means handling the failure must happen in the calling code. Because it is dynamically created in resolveUserTransaction(), this is possible, but if you had injected it directly into the strategy there would be no workaround other than copying, modifying, and renaming the source. BTW, when I first found this, I tried to find a way to coerce the plain @Resource annotation into working, either by mapping in web.xml, or trying to provide my own implementation of WELD's ResourceInjectionServices spi, but so far, I haven't been able to make it work. WELD's documentation on replacing that spi seems to be incorrect, though that's not your problem. I suppose my question is: is there some way in Deltaspike to cause the @Resource annotation to have the mappedName attribute dynamically added? I have already determined that the ProcessInjectionPoint event is never fired for @Resource fields (I tried to handle them in an extension); they are deferred to the WELD service, which I have been unable to customize. Dave --089e0153836216bd70051ecab6d2--