How to specify NLS when mybatis operates Oracle Database_ Language multilingual

Time:2021-7-29

1、 Background
When the application system connects to the Oracle database, the author needs to use English uniformly. The configuration cannot be modified on the database server side, because some methods need to be found on the application system side.
2、 Solution
After consulting the data source and mybatis related source code, there are two ways to support modification:
1. Use the configuration item connectioninitsqls of Druid data source to define the connection initialization statement:
connectionInitSqls: [“ALTER SESSION SET NLS_LANGUAGE=’AMERICAN'”]
2. Without modifying the data source configuration, add the interceptor of mybatis on the application system side. The interceptor code is as follows:
(1) Bean configuration of spring boot
@Configuration
public class MybatisPlusConfig {

/**
 *Change session state
 *
 * @return
 */
@Bean
public AlterSessionInterceptor alterSessionInterceptor() {
    return new AlterSessionInterceptor();
}

/**
 *Optimistic lock plug-in
 *
 * @return
 */
@Bean
public CustomOptimisticLockerInterceptor optimisticLockerInterceptor() {
    return new CustomOptimisticLockerInterceptor();
}

/**
 *Oracle sequence primary key
 *
 * @return
 */
@Bean
public OracleKeyGenerator oracleKeyGenerator() {
    return new OracleKeyGenerator();
}

}

(2) . interceptor Code:
@Slf4j
@Intercepts({@Signature(type = StatementHandler.class, method = “prepare”, args = {Connection.class, Integer.class})})
public class AlterSessionInterceptor implements Interceptor {

@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] args = invocation.getArgs();
    Connection connection = (Connection) args[0];
    if ("Oracle".equalsIgnoreCase(connection.getMetaData().getDatabaseProductName())) {
        Statement statement = null;
        try {
            statement = connection.createStatement();
            String locale = RequestHelper.getCurrentLocale();
            if ("en_GB".equalsIgnoreCase(locale)) {
                statement.execute("ALTER SESSION SET NLS_LANGUAGE='AMERICAN'");
            } else {
                statement.execute("ALTER SESSION SET NLS_LANGUAGE='SIMPLIFIED CHINESE'");
            }
        } finally {
            statement.close();
        }
    }
    return invocation.proceed();
}

@Override
public Object plugin(Object target) {
    if (target instanceof StatementHandler) {
        return Plugin.wrap(target, this);
    }
    return target;
}

@Override
public void setProperties(Properties properties) {
    // to do nothing
}

3、 Scheme comparison
1. In the data source configuration, it is configured at one time without modifying the code. All the error messages seen by all users are in English
2. When the code is intercepted, it is necessary to modify the code and display error prompts in different languages for different users. However, each time the statement is executed, it is necessary to execute an additional statement to set the language version, which is cumbersome and affects the performance

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]