• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / guice / #1107

21 Sep 2023 07:09PM UTC coverage: 80.045%. Remained the same
#1107

push

github

web-flow
Merge pull request #576 from sgdesmet/feat/guice7

Jakarta namespace and Guice 7

1408 of 1759 relevant lines covered (80.05%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/main/java/org/mybatis/guice/MyBatisJtaModule.java
1
/*
2
 *    Copyright 2009-2023 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.guice;
17

18
import jakarta.inject.Provider;
19
import jakarta.transaction.TransactionManager;
20
import org.apache.ibatis.logging.Log;
21
import org.apache.ibatis.logging.LogFactory;
22
import org.apache.ibatis.transaction.TransactionFactory;
23
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
24
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
25
import org.mybatis.guice.transactional.Transactional;
26
import org.mybatis.guice.transactional.TransactionalMethodInterceptor;
27
import org.mybatis.guice.transactional.TxTransactionalMethodInterceptor;
28
import org.mybatis.guice.transactional.XASqlSessionManagerProvider;
29

30
import javax.transaction.xa.XAResource;
31

32
import static com.google.inject.matcher.Matchers.annotatedWith;
33
import static com.google.inject.matcher.Matchers.any;
34
import static com.google.inject.matcher.Matchers.not;
35
import static org.mybatis.guice.Preconditions.checkArgument;
36

37
public abstract class MyBatisJtaModule extends MyBatisModule {
38
  private final Log log = LogFactory.getLog(getClass());
39

1✔
40
  private TransactionManager transactionManager;
41
  private Class<? extends Provider<? extends XAResource>> xaResourceProvider = XASqlSessionManagerProvider.class;
42

1✔
43
  public MyBatisJtaModule() {
44
  }
×
45

×
46
  public MyBatisJtaModule(TransactionManager transactionManager) {
47
    this.transactionManager = transactionManager;
1✔
48
  }
1✔
49

1✔
50
  @Override
51
  protected void bindTransactionInterceptors() {
52
    TransactionManager manager = getTransactionManager();
53

1✔
54
    if (manager == null) {
55
      log.debug("bind default transaction interceptors");
1✔
56
      super.bindTransactionInterceptors();
×
57
    } else {
×
58
      log.debug("bind XA transaction interceptors");
59

1✔
60
      // transactional interceptor
61
      TransactionalMethodInterceptor interceptor = new TransactionalMethodInterceptor();
62
      requestInjection(interceptor);
1✔
63

1✔
64
      // jta transactional interceptor
65
      TxTransactionalMethodInterceptor interceptorTx = new TxTransactionalMethodInterceptor();
66
      requestInjection(interceptorTx);
1✔
67
      bind(XAResource.class).toProvider(xaResourceProvider);
1✔
68

1✔
69
      bind(TransactionManager.class).toInstance(manager);
70

1✔
71
      bindInterceptor(any(), not(DECLARED_BY_OBJECT).and(annotatedWith(Transactional.class)), interceptorTx,
72
          interceptor);
1✔
73
      // Intercept classes annotated with Transactional, but avoid "double"
74
      // interception when a mathod is also annotated inside an annotated
75
      // class.
76
      bindInterceptor(annotatedWith(Transactional.class),
77
          not(DECLARED_BY_OBJECT).and(not(annotatedWith(Transactional.class))), interceptorTx, interceptor);
1✔
78
    }
1✔
79
  }
80

1✔
81
  protected TransactionManager getTransactionManager() {
82
    return transactionManager;
83
  }
1✔
84

85
  protected void setTransactionManager(TransactionManager transactionManager) {
86
    this.transactionManager = transactionManager;
87
  }
×
88

×
89
  protected void bindDefaultTransactionProvider() {
90
    Class<? extends TransactionFactory> factoryType = getTransactionManager() == null ? JdbcTransactionFactory.class
91
        : ManagedTransactionFactory.class;
1✔
92

1✔
93
    bindTransactionFactoryType(factoryType);
94
  }
1✔
95

1✔
96
  protected void bindXAResourceProvider(Class<? extends Provider<? extends XAResource>> xaResourceProvider) {
97
    checkArgument(xaResourceProvider != null, "Parameter 'xaResourceProvider' must be not null");
98
    this.xaResourceProvider = xaResourceProvider;
1✔
99
  }
1✔
100

1✔
101
  protected static class ProviderImpl<T> implements Provider<T> {
102
    private T wrapper;
103

104
    public ProviderImpl(T wrapper) {
105
      this.wrapper = wrapper;
1✔
106
    }
1✔
107

1✔
108
    @Override
109
    public T get() {
110
      return wrapper;
111
    }
1✔
112

113
  }
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc