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.
Related
I have configured a 2 dataSources in my SpringMVC Project, but when the 2nd
dataSource is unavailable it automatically uses 1st datasource where 2nd is referred.
I want to stop this switching.
Here is the code:
dispatcher-servlet.xml:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="defaultAutoCommit" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="initialSize" value="1"/>
<property name="maxActive" value="1"/>
<property name="maxWait" value="500"/>
<property name="maxIdle" value="2"/>
</bean>
<bean id="dataSource1" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false">
<property name="driverClassName" value="${jdbc.driverClassName1}"/>
<property name="url" value="${jdbc.url1}"/>
<property name="username" value="${jdbc.username1}"/>
<property name="password" value="${jdbc.password1}"/>
<property name="defaultAutoCommit" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="initialSize" value="1"/>
<property name="maxActive" value="3"/>
<property name="maxWait" value="500"/>
<property name="maxIdle" value="8"/>
</bean>
BaseNamedParameterJdbcDaoSupport.java class:
public class BaseNamedParameterJdbcDaoSupport extends NamedParameterJdbcDaoSupport{
#Autowired
public void setDataSourceFor1(DataSource dataSource) {
// System.out.println("Main DS"+dataSource);
setDataSource(dataSource);
}
}
BaseNamedParameterJdbcDaoSupportForMirrorDB.java :
public class BaseNamedParameterJdbcDaoSupportForMirrorDB extends NamedParameterJdbcDaoSupport{
#Autowired
public void setDataSourceFor2(DataSource dataSource1) {
// System.out.println("ForMirrorDB dataSource1"+dataSource1);
setDataSource(dataSource1);
}
}
It is not possible that the DataSource which are maintained with different name will automatically switch. I think you have implemented some other logic to work with these two DataSource objects.
Check your DAO/Service code to utilizing the "BaseNamedParameterJdbcDaoSupport" & "BaseNamedParameterJdbcDaoSupportForMirrorDB" with proper #Autowired.
I work on web java application using hibernate, and i always get memory low errors for tons of java inactive sessions on my oracle db. i tried to closing this unused sessions with c3p0 timeout config but it's not working at all.
this is my hibernate config and my open sessions after several minutes:
(the commented code below is for when i tried using common dbcp refer to one of answers for similar questions and timeout times set to 30 seconds for test.)
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:test</property>
<property name="hibernate.connection.username">tams_test</property>
<property name="hibernate.connection.password">test1234</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.connection.isolation">2</property>
<property name="org.hibernate.flushMode">COMMIT</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<property name="hibernate.order_updates">true</property>
<property name="hibernate.order_inserts">true</property>
<!-- <property name="hibernate.dbcp.initialSize">3</property>
<property name="hibernate.dbcp.maxIdle">50</property>
<property name="hibernate.dbcp.minIdle">0</property>
<property name="hibernate.dbcp.maxWait">180000</property>
<property name="hibernate.dbcp.maxConnLifetimeMillis">30000</property>
<property name="hibernate.dbcp.defaultQueryTimeout">1</property>-->
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">150</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.timeout">30</property>
<property name="hibernate.c3p0.idle_test_period">30</property>
<property name="hibernate.c3p0.idleConnectionTestPeriod">30</property>
<property name="c3p0.testConnectionOnCheckout">true</property>
<property name="testConnectionOnCheckin">true</property>
<property name="c3p0.maxConnectionAge">30</property>
<property name="maxIdleTimeExcessConnections">30</property>
<property name="connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
open sessions on oracle db after several minutes
I faced your problem too, and hibernate didn't want to apply my parameters of c3p0, so I changed my datasource and it completly solved my problem of inactive sessions.
Try to setup your dataSource bean like this:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:#xxxx:1521:xxx"/>
<property name="user" value="xx"/>
<property name="password" value="xxxx"/>
<property name="maxStatements" value="1000"/>
<property name="maxIdleTime" value="30"/>
<property name="maxPoolSize" value="100"/>
<property name="minPoolSize" value="10"/>
<property name="initialPoolSize" value="10"/>
<property name="idleConnectionTestPeriod" value="20"/>
</bean>
i am having a web application and my application is connected to database for every hit to my web application i need to make a connection to database so i want to reduce time by caching or pooling connection
below is my database configured file
xml file:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
Please use HikariCP for your application, best and very fast connection pool available in java world,configuration should be like below.
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" >
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/" />
<property name="maximumPoolSize" value="20" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="poolName" value="my-pool1" />
</bean>
</constructor-arg>
</bean>
Or you can have programatic configuration as well.
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");
config.setUsername("bart");
config.setPassword("51mp50n");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
HikariDataSource ds = new HikariDataSource(config);
You can use as follows , just change your database driver and url settings
<bean id="springDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="url" value="jdbc:oracle:thin:#localhost:1521:Employee" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="scott" />
<property name="password" value="tiger" />
<property name="removeAbandoned" value="true"/>
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
</bean>
You should use SingleConnectionDataSource as datasource
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
I currently use this configuration for my projects:
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource"
destroy-method="close">
<property name="URL" ...
<property name="user" ...
<property name="password" ...
<property name="connectionCachingEnabled" value="true" />
And it works fine, pretty fast.
I happened to see, on an old project (spring 2.5) this configuration:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url"...
<property name="username" ...
<property name="password" ...
</bean>
From documentation it would seem that this last option does not make use of a connection pool. I see no reason to use this configuration over mine, but it still exists so I am curious: where's the advantage/limitation?
1st configuration is oracle specific, whereas 2nd configuration is generic. You can explicitly define driver class.
This is the only major difference I can see in them other than connection pool support of OracleDataSource.
You can use it for generic behavior as mentioned below:
<bean id="baseDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
abstract="true">
<property name="username" value="user"/>
<property name="password" value="pwd" />
</bean>
<bean id="mySqlDataSource" parent="baseDataSource">
<property name="driverClassName" value="${mySQL.driver}" />
<property name="url" value="${mySQL.url}"/>
</bean>
<bean id="oracleDataSource" parent="baseDataSource">
<property name="driverClassName" value="${oracle.driver}" />
<property name="url" value="${oracle.url}"/>
</bean>
Property values you can externalized.
You can explore Apache Jakarta Commons DBCP which has all the features of DriverManagerDataSource
along with connection pool feature.
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