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

mybatis / guice / 1577

21 Apr 2025 11:15PM UTC coverage: 80.419% (-0.1%) from 80.529%
1577

push

github

web-flow
Merge pull request #821 from hazendaz/master

Cleanup throughout code for newer java styles

273 of 535 branches covered (51.03%)

39 of 41 new or added lines in 16 files covered. (95.12%)

1421 of 1767 relevant lines covered (80.42%)

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-2025 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 static com.google.inject.matcher.Matchers.annotatedWith;
19
import static com.google.inject.matcher.Matchers.any;
20
import static com.google.inject.matcher.Matchers.not;
21
import static org.mybatis.guice.Preconditions.checkArgument;
22

23
import jakarta.inject.Provider;
24
import jakarta.transaction.TransactionManager;
25

26
import javax.transaction.xa.XAResource;
27

28
import org.apache.ibatis.logging.Log;
29
import org.apache.ibatis.logging.LogFactory;
30
import org.apache.ibatis.transaction.TransactionFactory;
31
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
32
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
33
import org.mybatis.guice.transactional.Transactional;
34
import org.mybatis.guice.transactional.TransactionalMethodInterceptor;
35
import org.mybatis.guice.transactional.TxTransactionalMethodInterceptor;
36
import org.mybatis.guice.transactional.XASqlSessionManagerProvider;
37

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

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

NEW
44
  protected MyBatisJtaModule() {
×
45
  }
×
46

47
  protected MyBatisJtaModule(TransactionManager transactionManager) {
1✔
48
    this.transactionManager = transactionManager;
1✔
49
  }
1✔
50

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

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

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

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

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

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

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

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

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

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

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

102
  protected static class ProviderImpl<T> implements Provider<T> {
103
    private T wrapper;
104

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

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

114
  }
115
}
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

© 2026 Coveralls, Inc