MySQL - jdbc connection connects to incorrect schema. Fetched data from wrong schema - mysql

Project Technologies: Java spring hibernate + MySQL
I have created a replica schema say "abc_uat" from my production schema "abc" on the same server.
I changes connection string in Java code to point to new replica schema (abc_uat), as follows -
<property name="url" value="jdbc:mysql://abc.com:3306/ABC_UAT?autoReconnect=true" />
<property name="username" value="user" />
<property name="password" value="pass" />
Earlier the connection details used to be like this -
<property name="url" value="jdbc:mysql://abc.com:3306/ABC?autoReconnect=true" />
<property name="username" value="user" />
<property name="password" value="pass" />
Surprisingly, when I run this program, MySQL still connects with "abc" schema and not "abc_uat".
During debug, I can see Java code is properly creates connection to new schema (abc_uat), however, the data that is received back is from production schema( abc).
Here is how I had created replica -
I used export data into sql dump file on "abc" and then import data function to create tables and data into "abc_uat".
Can someone please point me, what am I missing?

Related

How to prevent database renewing its data in camunda

I am using
- camunda-bpm-wildfly-7.4.0
- mysql-5.6.24-winx64
- wildfly-8.2.1.Final
I have integerated mysql with camunda wildfly.
My datasource code in the standalone.xml is as follows,
<datasource jta="true" jndi-name="java:jboss/datasources/ProcessEngine" pool-name="ProcessEngine" enabled="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:mysql://localhost:8888/camundabpm?DB_CLOSE_DELAY=-1;MVCC=TRUE;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>mysql</driver>
How do I prevent the database resetting on server restart? It removes all data and replaces it with new data on every server start. So I am unable to persist my data between server on and off.
Update:
I found out that hibernate was deleting the tables from the db So I changed the property from this
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
to
<property name="hibernate.hbm2ddl.auto" value="update" />
after having a look at How to make Hibernate not drop tables. But now the tasklist process instances are removed and only the tables created by my project remain and all other tables are refreshed with new data.
Can you check the value of the databaseSchemaUpdate poroperty? It should be contained in the standalone.xml of our WildFly (very down). Make sure it os not set to create-drop.
See https://docs.camunda.org/manual/7.4/reference/deployment-descriptors/tags/process-engine/#configuration-properties

Settings for ComboPooledDataSource with JDBCTemplate

For accessing DB I have created datasource using com.mchange.v2.c3p0.ComboPooledDataSource with following properties
`
<property name="initialPoolSize" value="5" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="idleConnectionTestPeriod" value="200" />
<property name="acquireIncrement" value="1" />
<property name="maxStatements" value="25" />
<property name="numHelperThreads" value="3" />`
My dao class extends JDBCDaoSupport and above datasource is set into the JDBCDaoSupport which inturn provides JDBCTemplate for executing queries. I am not using any transaction manager.
I have two questions now:
How the transaction in managed? Does JDBCTemplate itself creates the transaction before insert ?
If database is not available, Initializing c3p0 pool goes into infinite wait..I suppose there should be some property which I can set as a timeout for initialization of pool.
I tried '
<property name="acquireRetryAttempts" value="1" />
but not sure if this the correct way of doing it.
I'm not sure if JDBCTemplate works with ComboPooledDataSource... I was trying to configure it with c3p0 but it was giving me warnings, the connections were not returned to the pool and new connections were created for each request. It just flooded my Mysql server (and my application with Acquisition Attempt, Too Many Connections Error)
Also I couldn't find anything on net regarding JDBCTemplate configuration except one tutorial on RoseIndia. following which produced those errors.
I would suggest using SessionFactory (or maybe you can prefer core jdbc PreparedStatement and Resultset) for your db transactions.
As far as Retry Attempts is concerned, I don't think c3p0 would go to infinite way by default configuration. You can use acquireRetryAttempts and acquireRetryDelay to set retry attempt time.
From the mchange site itself :
"When a c3p0 DataSource attempts and fails to acquire a Connection, it will retry up to acquireRetryAttempts times, with a delay of acquireRetryDelay between each attempt. If all attempts fail, any clients waiting for Connections from the DataSource will see an Exception, indicating that a Connection could not be acquired."
It would be better to go through the reference documentation from mchange and configure your c3p0 as per your needs.
mchange c3p0 reference

java.sql.SQLException: Already closed

We have a webapp running in production on tomcat with a MySQL back-end. All was fine for sometime, then suddenly we started getting this exception java.sql.SQLException: Already closed.
The entire stack trace is:
DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] Fetching JDBC Connection from DataSource
DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] Returning JDBC Connection to DataSource
DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] Could not close JDBC Connection
java.sql.SQLException: Already closed.
at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:114)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:191)
at org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:333)
at org.springframework.jdbc.datasource.DataSourceUtils.releaseConnection(DataSourceUtils.java:294)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:405)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:428)
at com.nokia.analytics.aws.aggregate.service.importer.DBInsert.truncateTable(DBInsert.java:135)
at com.blah.analytics.aggregate.service.importer.AggregateCollector.pullAndInsert(AggregateCollector.java:85)
at com.blah.analytics.aggregate.service.importer.AggregateCollector.call(AggregateCollector.java:96)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:679)
We are using org.apache.commons.dbcp.BasicDataSource as our datasource. I searched quite a bit but to no avail.
It doesn't occur always and hence is very hard to reproduce. It seems a problem with db connection pooling. Somewhere it was suggested to set this param as negative. Currently we are not changing of those parameters (all have default vals).
What approach should we follow to avoid it?
EDIT:
The relevant code is in (DBInsert.java)
133: String sql = "DELETE FROM "+tableName;
134: logger.debug(sql);
135: this.jdbcTemplate.execute(sql);
(133-135 are line nos. which are specified in the exception)
My datasource config:
<bean id="bisToolDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="${url}/blah_db?verifyServerCertificate=false&useSSL=true&requireSSL=true" />
<property name="username" value="${uname}" />
<property name="password" value="${passwd}" />
</bean>
The cause of this problem is connection isn't used in a long time, add testOnBorrow and validationQuery property to your datasource configuration then your application will work fine.
Good luck:)
As user NobodyElse pointed out, the problem was related to connection pooling. I was using org.apache.commons.dbcp.BasicDataSource as datasource. The nature of the application is such that there is spurt of connections at some fixed time in the day and no connections at all for the entire day. So due to this connections in the pool were getting stale and when next day application tried to connect to DB, we were getting this exception.
There are basically two solutions to this:
The one pointed out by NobodyElse, that is to use testOnBorrow; details can be found here
The other solution (which I employed for our app) is to turn off pooling completely. Note do this only when the application is not DB intensive (which was true in our case). So I switched to org.springframework.jdbc.datasource.DriverManagerDataSource. The config for which it seems to be working fine is:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="${url}/blah_db?verifyServerCertificate=false&useSSL=true&requireSSL=true" />
<property name="username" value="${uname}" />
<property name="password" value="${passwd}" />
</bean>

spring + hibernate + data base connection pooling and high availability

I'm trying to configure my web application like this:
1 tomcat server
2 mysql data bases in master/ master replication for failover purpose.
I also succeded to set up a pooled connexion using c3p0, thus avoiding to open a new connexion for each request(when no failover is involved)
I would like to use the failover support porvided by the jdbc connector in order to route request to the second database when the first one is unavailabe. Thus i'm using the fail over capability of the jdbc driver, simply by providing extra configuration parameters.
For the moment, when one server is unvailable, the requst are routed to the second available server. However, i'm facing a problem with a new opened connexion on the database for each request.
It seems that combaining connexion pooling and jdbc failover dosen't work toogther ?
Please advice, also regarding configuration parameters
<bean id="mydataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306,myOtherServer:3306/zeus?autoCommit=true&autoReconnect=true&autoReconnectForPools=true&failOverReadOnly=false"/>
<property name="user" value="root" />
<property name="password" value="root" />
<!-- these are C3P0 properties -->
<!-- property name="acquireIncrement" value="${acquireIncrement}" /_-->
<property name="minPoolSize" value="6" />
<property name="maxPoolSize" value="10" />
<property name="maxIdleTime" value="100" />
</bean>

getting java.io.EOFException when trying to insert data in mysql from spring mvc app

I am trying to submit data from spring mvc app to mysql . sometime i get the following error
org.springframework.dao.DataAccessResourceFailureException: StatementCallback; SQL [insert into networkwithusdb(company,firstname,lastname,email,address1,address2,city,state,zipcode,currentemployer,currentjobtitle,primarycareerarea,secondarycareerarea) values('HCA North Texas','dd','af','bhanukiran#imomentous.info','','','','KS','','','','Facilities','Facilities')]; Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.io.EOFException
STACKTRACE:
java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1963)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2375)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2874)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3243)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1343)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1260)
at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
at org.springframework.jdbc.core.JdbcTemplate$1UpdateStatementCallback.doInStatement(JdbcTemplate.java:508)
at org.springframework.jdbc.core.JdbcTemplate$1UpdateStatementCallback.doInStatement(JdbcTemplate.java:1)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:395)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:518)
at com.imomentous.hcanorthtexas.service.DatabaseService.insertNetworkData(DatabaseService.java:58)
at com.imomentous.hcanorthtexas.controller.NetworkWithUsController.processNetworkWithUSForm(NetworkWithUsController.java:62)
[... snip ...]
** END NESTED EXCEPTION **
only few times i get this exception.
following are my configuration file:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="url">
<value>${database.url}</value>
</property>
<property name="username">
<value>${database.username}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
<property name="maxActive" value="5" />
<property name="initialSize" value="3" />
<property name="validationQuery" value="SELECT 5 FROM DUAL" />
<property name="testOnBorrow" value="true" />
</bean>
and i am using the properties as:
database.driver =com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/XXXmobiledb
database.username=XXXX
database.password=XXXX
i am using:mysql-connector-java-5.0.5.jar
mysql server 5 .
sometimes database connection works properly and i get the result.
but sometimes it generates the error.
what might be the problem? If any have some idea please tell..thanks
The fact is that MySQL will disconnect the opened socket after a given time. You are advised to use a connection pool (such as c3p0).
See this post on the MySQL forum - http://forums.mysql.com/read.php?39,143312,180718#msg-180718. This should resolve it for you.
It might happen after a long time of inactivity add validation Query to your dataSource bean:
<property name="validationQuery">
<value>Select 1</value>
</property>
To avoid MySQL from disconnecting your session after a given idle time, you can use Spring OpenSessionInViewFilter or Interceptor to handle session.
This will require additional session filter settings in your web.xml file. You can mimic this post.