0

What is the equivalent to Hikari's connection-init-sql property?

spring:
  datasource:
    hikari:
      connection-init-sql: ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='DD/MM/YYYY HH24:MI:SSXFF TZR'

How to "translate" the snipet above to Atomikos?

spring:
  jta:
    atomikos:
      ?????: ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='DD/MM/YYYY HH24:MI:SSXFF TZR'

I'm using hibernate-core 5.4.33, spring-boot-starter-jta-atomikos 2.5.12 and java 11.

Cássio
  • 329
  • 3
  • 11

1 Answers1

0

Atomikos has not analog for this property, but we solved it using extended class

public class ExtendedAtomikosDataSourceBean extends AtomikosDataSourceBean {
    private String initSQL;
    private Map<Integer, Long> initializedConnections = null;
    private Long lastClearTime = new Date().getTime();

    -- pass here a value of connection-INIT-SQL property or what do you want
    public IB2AtomikosDataSourceBean(String initSQL) {
        this.initSQL = initSQL;
        if (initSQL!=null) initializedConnections = new ConcurrentHashMap<>();
    }

    private void clearConnections(){
        long now = new Date().getTime();
        for (Integer hashCode : initializedConnections.keySet()){
            Long lastUseTime = initializedConnections.get(hashCode);
            if (lastUseTime+1000*60*60*24<now) -- remove outdated 
                initializedConnections.remove(hashCode);
        }
        lastClearTime = now;
    }

    @Override
    public Connection getConnection() throws SQLException {
        Connection connection = super.getConnection();
        if (initializedConnections!=null) {
            int hashCode = connection.hashCode();
            if (lastClearTime+1000*60*60*24<new Date().getTime()) 
    -- time to clear map once a day or you can use another strategy comparing initializedConnections.size()>Max
                clearConnections();
            Long connInitializeDate = initializedConnections.get(hashCode);
            if (connInitializeDate == null) {
                PreparedStatement preparedStatement = connection.prepareStatement(initSQL);
                preparedStatement.execute();
                initializedConnections.put(hashCode, new Date().getTime());
            } else {
                initializedConnections.replace(hashCode, new Date().getTime());
            }
        }
        return connection;
    }
}