Return-Path: Delivered-To: apmail-ibatis-user-java-archive@www.apache.org Received: (qmail 31854 invoked from network); 23 Aug 2009 02:58:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 23 Aug 2009 02:58:24 -0000 Received: (qmail 15299 invoked by uid 500); 22 Aug 2009 22:54:45 -0000 Delivered-To: apmail-ibatis-user-java-archive@ibatis.apache.org Received: (qmail 15233 invoked by uid 500); 22 Aug 2009 22:54:45 -0000 Mailing-List: contact user-java-help@ibatis.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user-java@ibatis.apache.org Delivered-To: mailing list user-java@ibatis.apache.org Received: (qmail 15219 invoked by uid 99); 22 Aug 2009 22:54:45 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 22 Aug 2009 22:54:45 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of rickcr@gmail.com designates 74.125.78.147 as permitted sender) Received: from [74.125.78.147] (HELO ey-out-1920.google.com) (74.125.78.147) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 22 Aug 2009 22:54:37 +0000 Received: by ey-out-1920.google.com with SMTP id 13so347151eye.60 for ; Sat, 22 Aug 2009 15:54:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type:content-transfer-encoding; bh=UNK42Y/E2vLLqsHxaI27MBnaeFjd51y6OtBARe9hEdo=; b=e/ZTHRkU04vRKJ1TIgDMOwu9PVW4idtKkLF45OXp6ccXQdrC0afpa3RK6aoasfXm9e 8zq5Dp9vxZI4zTx5u2+RE7P1e8feksyJA3k5sO49ijzasKgrEFGyRz/MgYEpIXG0VOFx ibd+KK5EvmqMlzFcDYabPFg4YGMsAMiDDP7Qs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type :content-transfer-encoding; b=ezBWNVXmI/KhVo8CC5ot//WFHEox98m2uitxavJL0EegXr9VS3Xur4tFBF7ST9bpX5 glRNUn3R6TnyDDvRE3X/u2FDlSI/eIp1pf0jUh4IObT1fzmEe4E6EcQSF4YIXLFxtuEA VLZLubxmvE161MIaGxUpOSmtXAjo73GguWBw8= MIME-Version: 1.0 Received: by 10.216.86.145 with SMTP id w17mr578973wee.85.1250981655747; Sat, 22 Aug 2009 15:54:15 -0700 (PDT) Date: Sat, 22 Aug 2009 18:54:15 -0400 Message-ID: <583d4dff0908221554n77cf2b23id6361f5a2eb5a088@mail.gmail.com> Subject: Help creating a Guice TransactionInterceptor From: Rick To: ibatis-users Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org Just wondering if anyone has worked on a Guice transaction interceptor yet for i3 ? My progress so far... I want to avoid: public FooBar update(FooBar fooBar) { SqlSession session = sqlSessionFactory.openSession(ExecutorType.REUSE); try { FooBarMapper mapper = session.getMapper(FooBarMapper.class); mapper.update(fooBar); session.commit(); } finally { session.close(); } } So I started with an Interceptor like: public class TransactionInterceptor implements MethodInterceptor { @Inject SqlSessionFactory sqlSessionFactory; public Object invoke(MethodInvocation methodInvocation) throws Throwable { SqlSession session = sqlSessionFactory.openSession(ExecutorType.REUSE); try { return methodInvocation.proceed(); } finally { session.commit(); session.close(); System.out.println("session closed"); } } } //the annotation @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Transaction { } //sample usage @Transaction public Employee getEmployeeById(int id) { EmployeeMapper mapper = getSession().getMapper(EmployeeMapper.class); return mapper.getEmployeeById(id); } Couple problems though: 1) How can I pass the interceptor the correct SqlSessionFactory since you could have different ones for different DBs? In my Module I have: TransactionInterceptor transIntcp = new TransactionInterceptor(); requestInjection(transIntcp); bindInterceptor(Matchers.any(), Matchers.annotatedWith(Transaction.class),transIntcp); somehow I need a way to annotate a method with: @Transaction(@Named("MyDB")) and then end up with the correct sqlSessionFactory injected? I currently set up my different dbs with a different sqlSessionFactory using a provider like: SqlSessionFactoryProvider myDbProvider = new SqlSessionFactoryProvider("mydb-ibatis-config.xml"); bind(SqlSessionFactory.class).annotatedWith(Names.named("MyDB")) .toProvider(myDbProvider); 2) Look how I have to open the session twice.. once in the method call of a dao to get the Mapper and then again in the Interceptor so that I can close the session. This seems messed up for sure, although it's not really breaking anything, but I'm sure doing something improper behind the scenes. Anyone more familiar with guice have ideas how to tackle this? I'm a Guice newbie so I'm probably missing obvious things. --------------------------------------------------------------------- To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org For additional commands, e-mail: user-java-help@ibatis.apache.org