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

mybatis / generator / 1953

16 Jan 2026 06:20PM UTC coverage: 88.948% (-0.03%) from 88.976%
1953

push

github

web-flow
Merge pull request #1417 from jeffgbutler/sonar-nags

Fix Several Sonar Issues

2341 of 3164 branches covered (73.99%)

34 of 65 new or added lines in 17 files covered. (52.31%)

11581 of 13020 relevant lines covered (88.95%)

0.89 hits per line

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

85.71
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/ConnectionUtility.java
1
/*
2
 *    Copyright 2006-2026 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.generator.codegen;
17

18
import static org.mybatis.generator.internal.util.messages.Messages.getString;
19

20
import java.sql.Connection;
21
import java.sql.SQLException;
22

23
import org.mybatis.generator.config.ConnectionFactoryConfiguration;
24
import org.mybatis.generator.config.Context;
25
import org.mybatis.generator.config.JDBCConnectionConfiguration;
26
import org.mybatis.generator.exception.InternalException;
27
import org.mybatis.generator.internal.JDBCConnectionFactory;
28
import org.mybatis.generator.internal.ObjectFactory;
29

30
public final class ConnectionUtility {
31
    private ConnectionUtility() {
32
        // utility class - no instances
33
    }
34

35
    /**
36
     * This method creates a new JDBC connection from the values specified in the configuration file.
37
     *
38
     * <p>If you call this method, then you are responsible for closing the connection when you are done with it.
39
     * The best practice is to call the method in a try with resources to ensure proper cleanup.
40
     *
41
     * @param context the context containing the connection configuration
42
     * @return a new connection created from the values in the configuration file
43
     *
44
     * @throws SQLException if any error occurs while creating the connection
45
     */
46
    public static Connection getConnection(Context context) throws SQLException {
47
        // if both configs are null, it is an internal error - we should have caught that with validation
48

49
        JDBCConnectionConfiguration jdbcConfig = context.getJDBCConnectionConfiguration();
1✔
50
        if (jdbcConfig != null) {
1✔
51
            return new JDBCConnectionFactory(jdbcConfig).getConnection();
1✔
52
        } else {
53
            ConnectionFactoryConfiguration config = context.getConnectionFactoryConfiguration();
1✔
54
            if (config == null) {
1!
NEW
55
                throw new InternalException(getString("RuntimeError.23", context.getId())); //$NON-NLS-1$
×
56
            }
57

58
            return ObjectFactory.createConnectionFactory(config).getConnection();
1✔
59
        }
60
    }
61
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc