Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In HikariJNDIFactory, int[] type property of DataSource cannot be set. #2162

Open
m-takata opened this issue Jan 12, 2024 · 1 comment · May be fixed by #2291
Open

In HikariJNDIFactory, int[] type property of DataSource cannot be set. #2162

m-takata opened this issue Jan 12, 2024 · 1 comment · May be fixed by #2291

Comments

@m-takata
Copy link

I am trying to connect to AWS Aurora (Postgres) using HikariJNDIFactory on Tomcat 9.

Below is the META-INF/context.xml of the war to be deployed in Tomcat.

<Context>
    <Resource name="dataSourceForRead"
              uniqueName="dataSourceForRead"
              factory="com.zaxxer.hikari.HikariJNDIFactory"
              type="javax.sql.DataSource"
              dataSourceClassName="org.postgresql.ds.PGSimpleDataSource"
              auth="Container"
              minimumIdle="1" 
              maximumPoolSize="10"
              dataSource.url="jdbc:postgresql://xxxxx.cluster-xxxxx.ap-northeast-1.rds.amazonaws.com:5432,xxxxx.cluster-ro-xxxxx.ap-northeast-1.rds.amazonaws.com:5432/databasename"
              dataSource.serverNames="postgresql://xxxxx.cluster-xxxxx.ap-northeast-1.rds.amazonaws.com,xxxxx.cluster-ro-xxxxx.ap-northeast-1.rds.amazonaws.com"
              dataSource.portNumbers="5432,5432"
              dataSource.databaseName="databasename"
              dataSource.user="postgres"
              dataSource.password="xxxxxxxx"
              dataSource.sslMode="disable"
              dataSource.loginTimeout="2"
              dataSource.connectTimeout="2"
              dataSource.cancelSignalTimeout="2"
              dataSource.socketTimeout="60"
              dataSource.tcpKeepAlive="true"
              dataSource.loadBalanceHosts="true"
              dataSource.readOnly="true"
              dataSource.targetServerType="preferSecondary"
              dataSource.applicationName="web"/>

</Context>

Because there is a master and a read replica, there are multiple hostnames and port numbers for the database.

Therefore, I am trying to set them using the properties serverNames and portNumbers in DataSource.

However, the following exception occurred

java.lang.IllegalArgumentException: argument type mismatch
	com.zaxxer.hikari.util.PropertyElf.setProperty(PropertyElf.java:163)
	com.zaxxer.hikari.util.PropertyElf.lambda$setTargetFromProperties$0(PropertyElf.java:51)
	java.base\/java.util.concurrent.ConcurrentHashMap.forEach(ConcurrentHashMap.java:1603)
	java.base\/java.util.Properties.forEach(Properties.java:1422)
	com.zaxxer.hikari.util.PropertyElf.setTargetFromProperties(PropertyElf.java:46)
	com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:324)
	com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:113)
	com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:91)
	com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:80)
	com.zaxxer.hikari.HikariJNDIFactory.createDataSource(HikariJNDIFactory.java:63)
	com.zaxxer.hikari.HikariJNDIFactory.getObjectInstance(HikariJNDIFactory.java:51)
	org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:96)
	java.naming\/javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:342)
	org.apache.naming.NamingContext.lookup(NamingContext.java:864)
	org.apache.naming.NamingContext.lookup(NamingContext.java:158)
	org.apache.naming.NamingContext.lookup(NamingContext.java:850)
	org.apache.naming.NamingContext.lookup(NamingContext.java:158)
	org.apache.naming.NamingContext.lookup(NamingContext.java:850)
	org.apache.naming.NamingContext.lookup(NamingContext.java:172)
	org.apache.naming.SelectorContext.lookup(SelectorContext.java:161)
	java.naming\/javax.naming.InitialContext.lookup(InitialContext.java:409)

Looking at the HikariCP implementation, it seems that injections into int[] and String[] properties are not supported.
https://github.com/brettwooldridge/HikariCP/blob/dev/src/main/java/com/zaxxer/hikari/util/PropertyElf.java#L112-L165

I would like to be able to set the int[] and String[] properties of the DataSource as well.

@mjsmith707
Copy link

mjsmith707 commented Dec 19, 2024

Any movement on this? setServerName is deprecated in the Postgres JDBC driver so presumably this has to be supported at some point.

edit After much finagling I traced this down to my usage of quill-jdbc's JdbcContextConfig which just shoves everything into it's properties as a String. I replaced their implementation with my own and had to do some extra work to ensure that the serverPorts is actually a primitive int[] array instead of a boxed Integer array as the reflective call to Postgres' BaseDataSource.setPortNumbers wouldn't accept a boxed array.

Otherwise this appears to work just fine in Hikari

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants