Can't connect to PostgreSQL with JDBC driver - java

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"/>

Related

Unable to configure Hikaricp 2.2.5 with Java 6

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.

Connect to IPv6 mysql using java

I am using org.springframework.version -- >3.1.0.RELEASE and my datasource is like below
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="prototype">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="username" value="test"/>
<property name="password" value="test123"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="16000"/>
<property name="timeBetweenEvictionRunsMillis" value="30000"/>
<property name="minEvictableIdleTimeMillis" value="60000"/>
<property name="testOnBorrow" value="true"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
Here is below code to create jdbctemplate...
BasicDataSource bds = (BasicDataSource) ctx.getBean("dataSource");
bds.setUrl("jdbc:mysql://address=(protocol=tcp)(host=xx.xx.xx.xx)(port=3306 )/" + db + "?autoreconnect=true);
JdbcTemplate jt = new JdbcTemplate(bds);
I searched and this the url suggested to connect to IPv6 address. I am able to connect thro DriverManager.getConnection but not thro this datasource....Am i missing anything here
Exception
Caused by: java.net.UnknownHostException: address=(protocol=tcp)(host=xx.xx.xx.xx)
at java.net.InetAddress.getAllByName0(InetAddress.jav a:1215)
at java.net.InetAddress.getAllByName(InetAddress.java :1127)
at java.net.InetAddress.getAllByName(InetAddress.java :1063)
at com.mysql.jdbc.StandardSocketFactory.connect(Stand ardSocketFactory.java:243)
I am using Centos-6.2 x86_64, I have checked the /etc/hosts and everything looks fine.

Java program to connect mysql using socket=/opt/zimbra/db/mysql.sock

I have fetched my mail from gmail account to local zimbra machine and which is stored in mysql database, which run mysql on 7306 port and /opt/zimbra/db/mysql.sock.
Now, I would like to access all those record via java program.
I have used Spring with Java program using zimbra java version 1.7.0_07-b10
Here is configuration in xml file ..
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://172.18.100.117:7306/mboxgroup8?socket=/opt/zimbra/db/mysql.sock"/>
<property name="username" value="zimbra"/>
<property name="password" value="Zy"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
<property name="maxWait" value="60000"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
<property name="logAbandoned" value="true"/>
<property name="validationQuery" value="Select 1"/>
<property name="testOnBorrow" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="10000"/>
<property name="minEvictableIdleTimeMillis" value="60000"/>
</bean>
When I tried to connect via program, it gives me below error...
getMails- 59- Exception in getMails at Manager:org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:182)
at org.springframework.orm.ibatis.SqlMapClientTemplate.executeWithListResult(SqlMapClientTemplate.java:219)
at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:266)
at com.cashEmail.core.dao.MailItemDAOImpl.selectByExampleWithBLOBs(MailItemDAOImpl.java:86)
at com.cashEmail.manager.CashManager.getMails(CashManager.java:48)
at com.cashEmail.core.service.CashService.getCashEmail(CashService.java:77)
at com.cashEmail.resource.CashResource.getEmails(CashResource.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

spring Ibatis Mysql ReplicationDriver

i had use the spring3.1 ibatis2.x and mysql5.5.
then i wanna use the replication driver .
my code is:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.ReplicationDriver" />
<property name="url" value="jdbc:mysql:replication://192.168.1.160:3306/test,192.168.1.160:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
.....
then:
Caused by: org.apache.commons.dbcp.SQLNestedException:
Cannot create PoolableConnectionFactory (Must specify at least one slave host to connect to for master/slave replication load-balancing functionality)
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:224)
at $Proxy50.getMetaData(Unknown Source)
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:285)
... 50 more
Caused by: java.sql.SQLException: Must specify at least one slave host to connect to for master/slave replication load-balancing functionality
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.NonRegisteringReplicationDriver.connect(NonRegisteringReplicationDriver.java:107)
at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
so how to make the ibatis to support replication
Try this
<property name="url" value="jdbc:mysql:replication://192.168.1.160:3306,192.168.1.160:3306/test" />
The format is : master,slave1,slave2
Don't why you taking same server for load balancing.

How should I write JDBC url for Oracle with LDAP and two LDAP servers?

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>

Categories