简体   繁体   中英

setClientIdentifier on oracle Connection from java application

how to set client identifier to oracle connection from java application. My stack is hibernate 4.3.6 and spring 4.1.0. Transaction are managed by spring with annotation @Transactional

My first try does't work

    @Autowired
private QueryDslUtil queryDslUtil;

@Autowired
private SessionFactory sessionFactory;

@Override
@Transactional(readOnly = true)
public void test(String kod) {


    Session tmpSession = sessionFactory.getCurrentSession();
    tmpSession.doWork(connection -> {
        Properties prop = new Properties();
        prop.put("userId", "mito");
        connection.setClientInfo(prop);
    });


    Objects.requireNonNull(kod);
    QOrganizacia source = QOrganizacia.organizacia;
    queryDslUtil.from(source).where(source.kod.eq(kod)).uniqueResult(source);
}

my datasource is provided by weblogic server via jndi register or c3po in dev enviroment and I use oracle driver v 11.2.0.1.0

Thanks for help.

OraclConnection interface has dedicated method to do this. Some time ago it was setClientIdentifier but it is deprecated. Now it is setEndToEndMetrics . It allows to set up these columns in v$session . select MODULE, ACTION , CLIENT_IDENTIFIER from v$session;

      String metric[] = new String[OracleConnection.END_TO_END_STATE_INDEX_MAX];
      metric[OracleConnection.END_TO_END_ACTION_INDEX]   = "I'm ACTION ";
      metric[OracleConnection.END_TO_END_MODULE_INDEX]   = "i'm MODULE";
      metric[OracleConnection.END_TO_END_CLIENTID_INDEX] = "I'm CLIENT_IDENTIFIER";
      ((OracleConnection) connection).setEndToEndMetrics(metric, (short) 0);

solved on weblogic server:

@Override
@Transactional(readOnly = true)
public void log(String kod) {


    Session tmpSession = sessionFactory.getCurrentSession();
    tmpSession.doWork(connection -> {
        WebLogicNativeJdbcExtractor wlne = new WebLogicNativeJdbcExtractor();
        Connection oracleConnection = wlne.getNativeConnection(connection);
        LOGGER.debug("in work...");
        LOGGER.debug("is oracleConnection {}", oracleConnection instanceof OracleConnection);
        LOGGER.debug("oracleConnection casted {}", oracleConnection);
        String metric[] = new String[OracleConnection.END_TO_END_STATE_INDEX_MAX];
        metric[OracleConnection.END_TO_END_ACTION_INDEX] = "I'm ACTION ";
        metric[OracleConnection.END_TO_END_MODULE_INDEX] = "i'm MODULE";
        metric[OracleConnection.END_TO_END_CLIENTID_INDEX] = "user";
        ((OracleConnection )oracleConnection).setEndToEndMetrics(metric, (short) 0);
    });


    Objects.requireNonNull(kod);
    QOrganizacia source = QOrganizacia.organizacia;
    queryDslUtil.from(source).where(source.nazov.eq(kod)).uniqueResult(source);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM