I have a Spring java web application. I am trying to use Hikaricp 2.2.5 for connection pooling with the following configuration.
Maven:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java6</artifactId>
<version>2.2.5</version>
<scope>compile</scope>
</dependency>
XML:
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="minimumIdle" value="5"/>
<property name="maximumPoolSize" value="200"/>
<property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="connectionTestQuery" value="SELECT 1"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/app"/>
<property name="autoCommit" value="true"/>
<property name="connectionTimeout" value="30000"/>
<property name="idleTimeout" value="600000"/>
<property name="maxLifetime" value="1800000"/>
<property name="poolName" value="LoginPool"/>
</bean>
I am getting the following error NoSuchMethodError.
Exception Stack Trace:
Caused by: java.lang.NoSuchMethodError: java.sql.Connection.getNetworkTimeout()I
at com.zaxxer.hikari.util.PoolUtilities.isJdbc41Compliant(PoolUtilities.java:245)
at com.zaxxer.hikari.pool.HikariPool.addConnection(HikariPool.java:412)
at com.zaxxer.hikari.pool.HikariPool.fillPool(HikariPool.java:500)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:159)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:110)
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:102)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:76)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2006)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1289)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1419)
... 109 more
How to resolve the issue?
Couple of things. The exception in your stacktrace:
at com.zaxxer.hikari.pool.HikariPool.addConnection(HikariPool.java:412)
does not line up with a call to isJdbc41Compliant() in the 2.2.5 branch. That code was removed in 2.2.5. Second, if you are using 2.2.5 and you are setting jdbcUrl you do not need dataSourceClassName, they are mutually exclusive. Try a clean build, and as a last resort go into your .m2/repositories directory and find and delete all HikariCP artifacts and let them re-download.
UPDATE: lastly, make sure you are using the latest MySQL driver if possible.
Related
I have a spring application hosted on to the server (Tomcat 8.5). It goes idle if no one uses it. I already knew that timeout will occur if the DB is in idle state for 8 hours (Default timeout of MySQL). As mentioned in Spring Autoreconnect and Connection lost overnight post i have tried the solution available here.I have tried configuring application.properties but that doesn't bring any solution to the problem.
(PS:I'm not changing anything other than application.properties in my Spring Application).
Well if this
spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.validationQuery = SELECT 1
or this
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
didnt work maybe try this
Post SpringBoot 1.4 names have changed
They have defined new specific namespaces for the four connections pools spring supports: tomcat, hikari, dbcp, dbcp2.
spring.datasource.tomcat.testOnBorrow=true
spring.datasource.tomcat.validationQuery=SELECT 1
If problem doesn't solve even after including properties as in application.properties, Then problem will be solved when including testOnBorrow,validationQuery in application-context.xml located in src/main/resources
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.driver.classname}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="initialSize" value="2"/>
<property name="maxActive" value="50"/>
<property name="maxIdle" value="5"/>
<property name="maxWait" value="-1"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="600"/>
<property name="logAbandoned" value="true"/>
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="SELECT 1" />
</bean>
The solution is to validate connection thread when it is borrowed from thread pool by enabling testOnBorrow and providing validationQuery.
I want to set my db file location as part of system variable which shall be provided at time of starting application.
I want to access this system property in applicationContext.xml for which I have tried to use #{systemProperties['db.properties']}in applicationContext.xml.
I have defined propertyPlaceholderConfigurer bean in 2 following ways specifying value for property "locations" in 2 different ways:
Case 1 #:
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true"/>
<property name="locations">
<value>file://"#{systemProperties['db.properties']}"</value>
</property>
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
dataSource bean #:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.pwd}"/>
</bean>
Case 2 #:
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true"/>
<property name="locations">
<value>file://${DB_CONF}/test/db.properties</value>
</property>
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
If I use the value of property locations as in case 2 instead of
"#{systemProperties['db.properties']}" application works perfectly OK.
In the case 1, I try to access the same properties file from same location as a System property but application does not work with error as follows:
Stack Trace
Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:235)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171)
at org.hibernate.internal.SessionImpl.connection(SessionImpl.java:450)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:450)
... 133 more
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL '${db.url}'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
... 136 more
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:315)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
Here you find how to read properties file using xml
<bean id="propertyBean"
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<!-- This will find you file inside system classpath -->
<value>classpath:application.properties</value>
<!-- or you pass the whole path -->
<!--<value>file:///opt/application.properties</value> -->
</list>
</property>
</bean>
I could resolve the error.The culprit was double quotes.
Using file://#{systemProperties['db.properties']} instead of file://"#{systemProperties['db.properties']}" resolved the issue.
I want to use my Informix JDBC application over SSL. The DBMS is Informix IBM 11.70.
I'm using a spring-config.xml file in which I declare the datasource bean:
<!-- Data source -->
<bean id="dataSource" class="com.sopra.datasource.CustomDataSource" init-method="init"
destroy-method="close">
<property name="url" value="${url}" />
<property name="driverClassName" value="${driverClassName}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="removeAbandoned" value="true" />
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
</bean>
The problem is that when I deploy my application in Tomcat 7 everything goes well (both TCP and SSL mode), however, when I deploy it in JBOSS 7.1 the SSL connexion mode to the Informix database doesn't work!
When it comes to debugging, the only information that I have is the following :
Application side:
Caused by: com.informix.asf.IfxASFException: Attempt to connect to database server (my_server_ssl) failed.
at com.informix.util.IfxErrMsg.getLocIfxASFException(IfxErrMsg.java:751) [jdbc-4.10.7.20160517.jar:4.1.0.SNAPSHOT]
at com.informix.asf.Connection.openSocket(Connection.java:1864) [jdbc-4.10.7.20160517.jar:4.1.0.SNAPSHOT]
at com.informix.asf.Connection.<init>(Connection.java:427) [jdbc-4.10.7.20160517.jar:4.1.0.SNAPSHOT]
at com.informix.jdbc.IfxSqliConnect.<init>(IfxSqliConnect.java:1416) [jdbc-4.10.7.20160517.jar:4.1.0.SNAPSHOT]
... 47 more
Caused by: java.lang.NullPointerException
at com.informix.asf.Connection.getEnabledSSLProtocols(Connection.java:2242) [jdbc-4.10.7.20160517.jar:4.1.0.SNAPSHOT]
at com.informix.asf.Connection.openSocket(Connection.java:1805) [jdbc-4.10.7.20160517.jar:4.1.0.SNAPSHOT]
... 49 more
Thanks in Advance.
This was due to a defect in the 4.10.7 version of the driver. Try upgrading to either 4.10.8 or 4.10.9 versions. They can be found via maven or through your IBM download site.
Gradle
compile group: 'com.ibm.informix', name: 'jdbc', version: '4.10.9'
Maven
<dependency>
<groupId>com.ibm.informix</groupId>
<artifactId>jdbc</artifactId>
<version>4.10.9</version>
</dependency>
I have my Spring MVC app.
dispatcher-servlet.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/dailyjob"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
When my app is trying to connect to the database, I get the exception
Caused by: org.postgresql.util.PSQLException: ?????: ????????????
"root" ?? ?????? ???????? ??????????? (?? ??????) at
org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:398)
at
org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:173)
at
org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
at
org.postgresql.jdbc2.AbstractJdbc2Connection.(AbstractJdbc2Connection.java:136)
at
org.postgresql.jdbc3.AbstractJdbc3Connection.(AbstractJdbc3Connection.java:29)
at
org.postgresql.jdbc3g.AbstractJdbc3gConnection.(AbstractJdbc3gConnection.java:21)
at
org.postgresql.jdbc4.AbstractJdbc4Connection.(AbstractJdbc4Connection.java:31)
at
org.postgresql.jdbc4.Jdbc4Connection.(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393) at
org.postgresql.Driver.connect(Driver.java:267) at
java.sql.DriverManager.getConnection(DriverManager.java:664) at
java.sql.DriverManager.getConnection(DriverManager.java:208) at
org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173)
at
org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164)
at
org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:153)
at
org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119)
at
org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at
org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
... 46 more
If I'm trying to connect to mysql with
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost/dailyjob"
p:username="root"
p:password="root" />
everything is OK!
My app has jdbc drivres for both (mysql and postgres).
I am also uses postgresql with Java earlier I also facing the same problem,
I work around and found the solution, could you please try with adding below statement to bean "dataSource"
<property name="defaultAutoCommit" value="false"/>
I have an Oracle database with LDAP, and can connect by writing smth like:
jdbc:oracle:thin:#ldap://oid0.wow.com:666/chost,cn=OracleContext,dc=wow,dc=com
But this is not failover (we have another server with Oracle LDAP - oid1.wow.com:666)
How can I use both servers to reach failover?
I found this solution, but it doesn't work:
java.sql.SQLException: Io exception: Invalid LDAP URL specified
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:257)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:389)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:454)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:802)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
It was a driver!
Throw debugging I've found antother issue with String.concat() NPE
Our driver (com.oracle ojdbc14 10.2.0.4.0 - found in MVN repo) doesn't suppor two o more URL's.
When I downloaded driver from Oracle site (10.1.0.5 'ojdbc14_g') all start work fine.
All of this looks strange because 10.1.0.5 seems to be the last version of Oracle JDBC drivers on official site (for Oracle 10 of course).
Here is! Failover dataSource bean:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!--common dataSource props -->
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:#ldaps://oid0.wow.com:666/chost,cn=OracleContext,dc=wow,dc=com ldaps://oid1.wow.com:666/chost,cn=OracleContext,dc=wow,dc=com"/>
<property name="overrideDefaultUser" value="useruser"/>
<property name="overrideDefaultPassword" value="strongpassword"/>
<!-- special c3p0 props -->
<property name="minPoolSize" value="1"/>
<property name="maxPoolSize" value="10"/>
<property name="maxStatements" value="10"/>
<property name="breakAfterAcquireFailure" value="false"/>
<property name="acquireRetryAttempts" value="5"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="properties">
<props>
<prop key="oracle.net.ldap_loadbalance">OFF</prop>
</props>
</property>
</bean>