HikariCP with PostgreSQL: setQueryTimeout(int) is not yet implemented - java

I tried to use HikariCP with PostgreSQL but it doesn't work. It throws an exception:
WARN [2014-10-24 14:38:54,195] com.zaxxer.hikari.pool.HikariPool: Exception during keep alive check, that means the connection must be dead.
! org.postgresql.util.PSQLException: Method org.postgresql.jdbc4.Jdbc4Statement.setQueryTimeout(int) is not yet implemented.
! at org.postgresql.Driver.notImplemented(Driver.java:753) ~[postgresql-9.0-801.jdbc4.jar:na]
! at org.postgresql.jdbc2.AbstractJdbc2Statement.setQueryTimeout(AbstractJdbc2Statement.java:656) ~[postgresql-9.0-801.jdbc4.jar:na]
! at com.zaxxer.hikari.pool.HikariPool.isConnectionAlive(HikariPool.java:473) [HikariCP-java6-2.1.0.jar:na]
! at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:178) [HikariCP-java6-2.1.0.jar:na]
! at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:94) [HikariCP-java6-2.1.0.jar:na]
I tried all possible driver versions (including org.postgresql:postgresql:9.3-1102-jdbc41) - nothing helped. Did anybody use it with PostgreSQL?
Here is my config file:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource" />
<property name="maximumPoolSize" value="${jdbc.maximumPoolSize:20}" />
<property name="idleTimeout" value="${jdbc.idleTimeout:600000}" />
<property name="dataSourceProperties">
<props>
<prop key="databaseName">vidsearch_test</prop>
<prop key="user">${hibernate.connection.username}</prop>
<prop key="password">${hibernate.connection.password}</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig"/>
</bean>

Don't use a connectionTestQuery. Allow HikariCP to follow its default behavior of using the Connection.isValid() method. This requires a JDBC4 driver.

Version 42.2.0 (or higher) implements set/get network timeout.

Related

Hibernate H2 database locks after project rebuild

I am setting up a Spring hibernate H2 application. When the server starts everything works but after an update (rebuild project in eclipse without restarting Tomcat) I get the following error messages saying that the database file can't be accessed.
Error Messages:
java.lang.IllegalStateException: The file is locked: nio:/home/bob/dataStore.mv.db [1.4.187/7]
java.nio.channels.OverlappingFileLockException
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Database may be already in use:
After Googling this error I tried adding File_LOCK=NO and DB_CLOSE_ON_EXIT=TRUE to the URL but no luck.
Context.xml file
<context:annotation-config />
<context:component-scan base-package="com" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<!-- <property name="url" value="jdbc:h2:tcp://localhost/~/dataStore;FILE_LOCK=NO;DB_CLOSE_ON_EXIT=TRUE" /> -->
<property name="url" value="jdbc:h2:file:~/dataStore;FILE_LOCK=NO;DB_CLOSE_ON_EXIT=TRUE;MVCC=TRUE" />
<property name="username" value="" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
What can I do so that the database does not get locked every time the project is rebuilt.
Also, eclipse is rebuilding the application after everytime the database is updated. How can I stop this?
Try DB_CLOSE_ON_EXIT=FALSE, from the Spring docs
If, for whatever reason, you do configure the connection URL for an embedded database, care should be taken to ensure that the database’s automatic shutdown is disabled. If you’re using H2 you should use DB_CLOSE_ON_EXIT=FALSE to do so.
if need to create multipul connecation then you need to use below code :
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/C:\Database\Data\production;"/>

Error on configuration of JNDI lookup for WebLogic + Spring

I'm migrating an application from OC4J to WebLogic 12c and the Spring beans are giving an error I can't figure out how to solve. My question is what can be the cause of this error.
I have the following bean for the JNDI lookup:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${datasource.jndiname}" />
<property name="lookupOnStartup">
<value>false</value>
</property>
<property name="proxyInterface">
<value>javax.sql.DataSource</value>
</property>
</bean>
The value ${datasource.jndiname} is expected to come from a config.properties file with the following line:
server.database.datasource=${datasource.jndiname}
And the value of server.database.datasource comes from a config.filter file with the line:
server.database.datasource=jdbc/DATASOURCE
This works fine with OC4J and it also works when I replace the ${datasource.jndiname} to its value jdbc/DATASOURCE in WebLogic, but it gives me the following error if I keep the reference (and I need to keep it):
JndiObjectTargetSource failed to obtain new target object; nested exception is javax.naming.NameNotFoundException: While trying to lookup '${datasource.jndiname}' didn't find subcontext '${datasource'. Resolved ''; remaining name '${datasource/jndiname}'
After some research, I found a property for the bean that fixed the error. Setting the property resourceRef to false makes the reference ${datasource.jndiname} work as expected.
In the final code shown below I also added a JndiTemplate.
<bean id="dsJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.provider.url">t3://localhost:7001</prop>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${datasource.jndiname}" />
<property name="resourceRef" value="false"/>
<property name="lookupOnStartup" value="false"/>
<property name="proxyInterface" value="javax.sql.DataSource"/>
<property name="jndiTemplate">
<ref local="dsJndiTemplate" />
</property>
</bean>

HSQL file based database from script

I have a question. I was playing around with JSF/ Spring/ Hibernate and HSQL.
I have a small test application for this purpose and for one feature I wanted to implement I needed a possiblity to persist some data. I didn't wanted to have a full fledged database, so I choose HSQL. Starting from this tutorial (http://devcrumb.com/hibernate/hibernate-jpa-spring-and-hsqldb) I build my application. I had some problems with datatypes and I have chosen to build my database from a script.
Like in the tuorial I had defined my datasource like this in my application.xml, except that I had changed the url property to use a file.
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:hsql://localhost/testdb</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="jpaData" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
Now, I have changed the datasource to something like this:
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:embeddedDbStructure.sql" />
</jdbc:embedded-database>
Everything works fine so far.
My problem is, that this database is deleted when the server is shut down. Is there a possibility to use both? A script as base for the structure of my database but the db itself is written to a file on my filesystem so that it is available after a server restart? I know there are propably a few solutions to achieve this via some code, but is there a possibility to configure it that way?
Thanks in advance
Actually for your case, you are using the hsqldb in-memory mode, the spring is house a inmemory database indeed. To reuse the file database you need to update the hsqldb mode to use the "In-Process Mode" like "jdbc:hsqldb:file:testdb"
And a sample question is Embedded HSQLDB persist data to a file, you can refer to this.

Configuring properties via datasource vs configuring them via hibernateProperties

What is the difference between using datasource and using hibernateProperties. I want to use c3P0 with spring in my app. I found 2 ways to do so but I'm unable to understand the difference between the two
First:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
depends-on="dataSource">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
</props>
</property>
<bean id="dataSource" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="maxPoolSize" value="10" />
<property name="numHelperThreads" value="5" />
</bean>
Second:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
>
<property name="hibernateProperties">
<props>
<property name="hibernate.c3p0.maxSize" value="100" />
<property name="hibernate.c3p0.numHelperThreads" value="5" />>
</props>
</property>
</bean>
The first you get a Spring managed datasource, which you can also use for a JdbcTemplate or other work.
The second you get a hibernate managed datasource which is not reusable by Spring.
I strongly suggest the first approach as it also makes it quite easy to replace your datasource for testing (by replacing it with an in-memory database) or to replace it with a JNDI lookup.

how to get a spring declared datasource in an xml config file

i have a spring datasource which looks like this:
<bean id="dataSource1" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database1.url}" />
<property name="username" value="${database1.username}" />
<property name="password" value="${database1.password}" />
</bean>
i need to make this available on jndi or jee. some related things:
<bean id="dataSourceJNDI1" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/dataSource1"/>
<property name="beanClassLoader" ref="dataSource1"></property>
</bean>
or maybe :
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/database1" />
not sure how to get the jndi or jee working with the dbcp. any help would be appreciated.
thanks in advance.
ps: guys this is a special needl. so i have to do it like this. please dont post unnecessary advices saying why i should use tomcat server as datasource. i am aware of setting datasource that way. i repeat again this is a special need. also please dont provide me java code solutions, not required.
ps: those who dont know how to do it, please do not occupy the space of this post saying its not possible. if you dont know the answer no need to post and junk the post.
You would need to bind the DataSource to the JNDI tree. You would need to supply the parameters for connecting to the local JNDI tree. JndiTemplate can do this. Some of the JNDI environment properties probably won't be necessary for a local InitialContext. I think "java.naming.factory.initial" is the only required. The other are for connecting to an out of process JNDI server:
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.provider.url">${jndi.provider.url}</prop>
<prop key="java.naming.factory.initial">${jndi.factory.initial}</prop>
<prop key="java.naming.security.principal">${jndi.security.principal}</prop>
<prop key="java.naming.security.credentials">${jndi.security.credentials}</prop>
</props>
</property>
</bean>
<bean factory-bean="jndiTemplate" factory-method="bind">
<constructor-arg type="java.lang.String" value="java:com/env/DataSoure"/>
<constructor-arg type="java.lang.Object" ref="dataSource"/>
</bean>
If you are performing a JNDI lookup in the same Spring context, you will either need to have the JNDI bean depends-on this lookup bean or make the JNDI lookup lazy so that it will perform the lookup on first use.
Since you are using jndi you have to declare the datasource as a jndi source.
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/database1" />
<bean id="dataSource1" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="dataSource" ref="dataSource" />
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database1.url}" />
<property name="username" value="${database1.username}" />
<property name="password" value="${database1.password}" />
</bean>
That should work assuming your bean definition for dataSource1 is correct.

Categories