【问题标题】:Can't alter Oracle session with JdbcTemplate无法使用 JdbcTemplate 更改 Oracle 会话
【发布时间】:2020-01-23 20:03:42
【问题描述】:

我需要在 Spring-Standalone 应用程序中更改 Oracle 会话的 NLS_NUMERIC_CHARACTERS。我正在使用 JdbcTemplates:

logger.info("Setting NLS-Parameter.");
jdbcTemplate.execute("alter session set NLS_NUMERIC_CHARACTERS='.,'");
logger.info(jdbcTemplate.queryForObject("select value from nls_session_parameters where parameter = 'NLS_NUMERIC_CHARACTERS'", String.class));

但它没有改变:

Setting NLS-Parameter.
,.

我以编程方式初始化 JdbcTemplate:

DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(ORACLE_JDBC_DRIVER);
dataSource.setUrl(url);
dataSource.setUsername(schema);
dataSource.setPassword(password);
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);

所有其他语句都运行良好。仅仅改变会话是行不通的。数据库是 Oracle 18 标准。

【问题讨论】:

标签: spring oracle spring-jdbc jdbctemplate oracle18c


【解决方案1】:

DriverManagerDataSource 数据源从每个 getConnection 调用中返回一个新的连接。

因此,JdbcTemplate#execute(final String sql)JdbcTemplate#queryForObject(String sql, Class<T> requiredType) 最终都调用的 JdbcTemplate#execute(StatementCallback<T> action) 将在每次执行时获得一个 newly created connection

alter session set NLS_NUMERIC_CHARACTERS='.,' 语句为 session 更改了NLS_NUMERIC_CHARACTERS,因此仅适用于执行该语句的连接。此更改不会影响其他连接。

【讨论】:

  • 作为一个即时解决方案,您可以切换到使用org.springframework.jdbc.datasource.SingleConnectionDataSource(将true 传递给最后一个构造函数参数suppressClose);假设您不需要多个并发连接
猜你喜欢
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 2010-11-28
相关资源
最近更新 更多