I'm using a spring datasource and is unable to connect to Oracle AQ Queue.
Connection connection = null;
AQSession aqSess = null;
connection = ds.getConnection();
connection.setAutoCommit(false);
DataSourceUtils.getTargetConnection(connection);
Class.forName("oracle.AQ.AQOracleDriver");
aqSess = AQDriverManager.createAQSession(connection);
aqSession = aqSess;
But still get this: oracle.jms.AQjmsException: JMS-112: Connection is invalid any tips would be appreciated.
<bean id="myId" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#myIpAddress:dev"/>
<property name="username" value="user"/>
<property name="password" value="pass"/>
<property name="removeAbandoned" value="true"/>
<property name="initialSize" value="2"/>
<property name="maxIdle" value="8"/>
<property name="maxActive" value="30"/>
<property name="maxWait" value="60000"/>
</bean>
AQException: oracle.AQ.AQException: JMS-112: Connection is invalid
at oracle.AQ.AQDriverManager.createAQSession(AQDriverManager.java:193)
I fixed my problem using the following code:
OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);
Recording it here as an answer in case others find this question when having a similar problem.
Oracle AQ doesn't match with BasicDataSource
useOracleDataSource instead
but if you want to use BasicDataSource:
In many application server environments the JDBC connection is wrapped
in an implementation specific class that delegates to the underlying
native JDBC connection. Oracle's AQ connection factory needs the
native Oracle connection and will throw an "oracle.jms.AQjmsException:
JMS-112: Connection is invalid" exception if the connection is wrapped
by a foreign class. To solve this problem you can specify a
NativeJdbcExtractor that can be used to unwrap the connection. Spring
provides a number of implementations to match the application server
environment. Here is an example for specifying a NativeJdbcExtractor.
<orcl:aq-jms-connection-factory id="connectionFactory"
use-local-data-source-transaction="true"
native-jdbc-extractor="dbcpNativeJdbcExtractor" 1
data-source="dataSource" />
<bean id="dbcpNativeJdbcExtractor"
class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>
<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
https://docs.spring.io/spring-data/jdbc/old-docs/2.0.0.BUILD-SNAPSHOT/reference/html/orcl.streamsaq.html
Related
I have an application that uses MySQL fail-over via a connection url:
jdbc:mysql://172.17.0.4:3306,172.17.0.3:3306/db_name?autoReconnect=true&failOverReadOnly=false
Now when primary db is down then it should move to secondary connection and application flow should work as expected.
Now the problem happens when moving to secondary db. It takes too long to move/execute the queries, causing the flow to take much longer than expected.
I have checked already with db and there's no slow query issues. I guess it's something to do with fail-over and with checking the states. So, any idea what might be causing this issue or how to resolve this delay?
I am also using c3p0 to manage the connections pools. Already tried with initialTimeout and maxReconnects but no luck so far.
DataSource
<bean id="productionDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="description" value="integration_ds"/>
<!-- configuration pool via c3p0-->
<property name="acquireIncrement" value="${datasource.acquireIncrement}"/>
<property name="idleConnectionTestPeriod" value="${datasource.idleConnectionTestPeriod}"/>
<!-- seconds -->
<property name="maxPoolSize" value="${datasource.maxPoolSize}"/>
<property name="maxStatements" value="${datasource.maxStatements}"/>
<property name="maxStatementsPerConnection" value="${datasource.maxStatementsPerConnection}"/>
<property name="minPoolSize" value="${datasource.minPoolSize}"/>
<property name="initialPoolSize" value="${datasource.initialPoolSize}"/>
<property name="maxIdleTime" value="${datasource.maxIdleTime}"/>
<property name="acquireRetryAttempts" value="${datasource.acquireRetryAttempts}"/>
<property name="acquireRetryDelay" value="${datasource.acquireRetryDelay}"/>
<property name="breakAfterAcquireFailure" value="${datasource.breakAfterAcquireFailure}"/>
<property name="debugUnreturnedConnectionStackTraces" value="true"/>
</bean>
Properties
datasource.acquireIncrement=1
datasource.idleConnectionTestPeriod=1000
datasource.maxPoolSize=10
datasource.maxStatements=600
datasource.minPoolSize=5
datasource.initialPoolSize=5
datasource.maxIdleTime=7200
#datasource.acquireRetryAttempts=5
datasource.acquireRetryAttempts=1
#datasource.acquireRetryDelay=5000
#datasource.acquireRetryDelay=1000
datasource.acquireRetryDelay=100
datasource.breakAfterAcquireFailure=false
datasource.maxStatementsPerConnection=3
datasource.checkoutTimeout=100
DAO
private static final String findByAppIdHql = "select app from AppImpl app where app.appId = ?";
final Query query = sf.getCurrentSession().createQuery(findByAppIdHql).setString(0, appId);
query.setCacheable(true);
query.setCacheRegion("app_query_cache");
query.setCacheMode(CacheMode.NORMAL);
List<App> apps = query.list();
I have implemented spring jdbc in my project. I am just curious to know how connection pooling in handled in spring jdbc? If spring is taking care of connections, then where can I specify the max number of connections allowed for my application?
Another question is how is connection pooling handled in simple jdbc.
Please clarify.
You can use own custom datasource, like this:
<bean id="springDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="url" value="jdbc:oracle:thin:#localhost:1521:SPRING_TEST" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="removeAbandoned" value="true"/>
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
</bean>
//Dao class configuration in spring
<bean id="EmployeeDatabaseBean" class="com.test.EmployeeDAOImpl">
<property name="dataSource" ref="springDataSource"/>
</bean>
Official documentation
Here or here described pretty simple.
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.
I'm trying to find the best way to create a dataSource in Spring for connecting to a Google Cloud SQL instance.
I'm currently using:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.GoogleDriver" />
<property name="url" value="jdbc:google:mysql://myappid:instanceId/mydb?user=myuser" />
<property name="username" value="myuser" />
<property name="password" value="mypassword" />
</bean>
However, I'm a little concerned about using the DriverManagerDataSource provided by Spring as it's documentation says it creates a new connection for every call.
Before migrating over to App Engine I was using a connection pool called BoneCP - however it uses classes that are restricted by App Engine. Is there a connection pool or some other data source class that is recommended to be used with Google Cloud SQL?
Try c3p0 or commons-dbcp. They both implement javax.sql.Datasource which is whitelisted by app-engine.
Example on commons-dbcp:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.GoogleDriver" />
<property name="url" value="jdbc:google:mysql://myappid:instanceId/mydb?user=myuser" />
<property name="username" value="myuser" />
<property name="password" value="mypassword" />
<property name="validationQuery" value="SELECT 1"/>
</bean>
At this moment I'm using DriverManagerDataSource with #Transactional annotation to manage transactions. But all transactions are very very slow, probably because data source open and close connection to db each time.
What data source should I use to speed up transaction?
I am using in my application combination of two approaches. the first one is c3p0 connection pooling, its almost the same solution as chkal sugested. The second approach is to use Spring lazyConnectionDataSourceProxy, which creates lazy loading proxy that loads connection only if you hit the database. This is very useful, when you have second level cache and you are only reading cached data and queries - database wont be hit, and you don't need to acquire connection (which is pretty expensive).
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Pool properties -->
<property name="minPoolSize" value="5" />
<property name="initialPoolSize" value="10" />
<property name="maxPoolSize" value="50" />
<property name="maxStatements" value="50" />
<property name="idleConnectionTestPeriod" value="120" />
<property name="maxIdleTime" value="1200" />
</bean>
<bean name="lazyConnectionDataSourceProxy" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="dataSource" />
</bean>
DriverManagerDataSource isn't actually a connection pool and should only be used for testing. You should try BasicDataSource from Apache Commons DBCP. Something like:
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>