connection.prepareStatement("alter session set container=YPDB2").executeUpdate() return 0;

connection.prepareStatement("alter session set container=YPDB2").executeUpdate() return 0;

使用ojbc7连接oracle12c, 执行"alter session set container=ypdb2",好像不行;

但是我用sqlplus来执行,可以用;

这是我的代码;

OracleDataSource oracleDataSource = new OracleDataSource();
oracleDataSource.setURL("jdbc:oracle:thin:@127.0.0.1:1521/orcl");
Connection connection = oracleDataSource.getConnection("sys as sysdba", "123456");
PreparedStatement preparedStatement = connection.prepareStatement("alter session set container=YPDB2");
log.info("{}",preparedStatement.executeUpdate());

控制台打印 0 它似乎影响了零行;

这是否意味着“改变”没有成功?

好像是ojbc的bug;

@Test
public void testAlterSession() throws SQLException {
    OracleDataSource oracleDataSource = new OracleDataSource();
    oracleDataSource.setURL("jdbc:oracle:thin:@127.0.0.1:1521/ypdb9");
    Connection sys_as_sysdba_connection = oracleDataSource.getConnection("sys as sysdba", "123456");
    int i = sys_as_sysdba_connection.createStatement().executeUpdate("alter session set container=ypdb9");
    log.info("i:{}",i);
    List<Map<String, Object>> maps = OdbcUtil.resultSetToList(sys_as_sysdba_connection.createStatement().executeQuery("select * from v$pdbs"));
    maps.forEach(map->log.info("{}",map));
    sys_as_sysdba_connection.createStatement().executeUpdate("alter session set container=cdb$root");
    List<Map<String,Object>> maps2 = OdbcUtil.resultSetToList(sys_as_sysdba_connection.createStatement().executeQuery("select * from v$pdbs"));
    maps2.forEach(map->log.info("{}",map));
}

在这个测试中,"excuteUpdate(sql)" returns 0,但是当我再次 select pdb 时 "container" 确实改变了;

我还不知道为什么;