Search code examples
sessionjdbcoracle12calter

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


use ojbc7 to connection oracle12c, execute "alter session set container=ypdb2", it seems not work;

but i use sqlplus to execute, it is work;

here is my code;

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());

console print 0 it seems to affect the zero row;

Does this mean that the “alter” did not succeed?


Solution

  • it seems a bug of ojbc;

    @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));
    }
    

    In this test, "excuteUpdate(sql)" returns 0,but the "container" does change when I select the pdb again;

    i don't know why yet;