How to Test/POC application continuity concept. I tried making the code to hold connections so that the application is not able get the connections and it tries to get from the pool. I am sure this is not the right way of testing application continuity, but I don't know how to create a proper scenario.
Is it the only way to bring down db while a transaction is in progress ?
I used the below code to simulate AC where a procedure is called 20 times in 20 threads with a new connection everytime. I am creating a scenario where one thread goes and waits for a connection, times-out as not getting connection in time, and then retires to get a connection, IS This a proper AC test. (Below is test class)
package com.ac;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.ValidConnection;
import java.sql.ResultSet;
public class App {
public static void main(String[] args) {
try {
new App().acSimulation();
} catch (Exception e) {
e.printStackTrace();
}
}
public void acSimulation() throws Exception{
final PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
pds.setConnectionFactoryClassName("oracle.jdbc.replay.OracleDataSourceImpl");
System.out.println("connection factory set");
String URL = "jdbc:oracle:thin:#(DESCRIPTION = (TRANSPORT_CONNECT_TIMEOUT=3) (RETRY_COUNT=20)(FAILOVER=ON) " +
" (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = " +
" (SERVER = DEDICATED) (SERVICE_NAME=orcl)))";
System.out.println("Using URL\n" + URL);
pds.setURL(URL);
pds.setUser("system");
pds.setPassword("oracle");
pds.setInitialPoolSize(1);
pds.setMinPoolSize(1);
pds.setMaxPoolSize(3);
pds.setConnectionWaitTimeout(10);
// RAC Features
pds.setConnectionPoolName("Application Continuity Pool");
pds.setFastConnectionFailoverEnabled(true);
// use srvctl config nodeapps to get the ONS ports on the cluster
// pds.setONSConfiguration("nodes=192.168.100.30:6200,192.168.100.32:6200");
System.out.println("pool configured, trying to get a connection");
// final Connection conn = null;
final Connection conn = pds.getConnection();
if (conn == null || !((ValidConnection) conn).isValid()) {
System.out.println("connection is not valid");
throw new Exception ("invalid connection obtained from the pool");
}
if ( conn instanceof oracle.jdbc.replay.ReplayableConnection ) {
System.out.println("got a replay data source");
} else {
System.out.println("this is not a replay data source. Why not?");
}
System.out.println("got a connection! Getting some stats if possible");
oracle.ucp.jdbc.JDBCConnectionPoolStatistics stats = pds.getStatistics();
System.out.println("\tgetAvailableConnectionsCount() " + stats.getAvailableConnectionsCount());
System.out.println("\tgetBorrowedConnectionsCount() " + stats.getBorrowedConnectionsCount() );
System.out.println("\tgetRemainingPoolCapacityCount() " + stats.getRemainingPoolCapacityCount());
System.out.println("\tgetTotalConnectionsCount() " + stats.getTotalConnectionsCount());
System.out.println(((oracle.ucp.jdbc.oracle.OracleJDBCConnectionPoolStatistics)pds.getStatistics()).getFCFProcessingInfo());
System.out.println("Now working");
int i=0;
while(i < 20){
new Runnable() {
public void run() {
try {
seperateInstance(conn, pds);
} catch (SQLException e) {
e.printStackTrace();
}
}
}.run();
i++;
}
}
private void seperateInstance(Connection conn, PoolDataSource pds) throws SQLException{
java.sql.CallableStatement cstmt = null;
oracle.ucp.jdbc.JDBCConnectionPoolStatistics stats = pds.getStatistics();
conn = pds.getConnection();
cstmt = conn.prepareCall("{call P_M_emp(?,?)}");
cstmt.setLong(1, 1);
cstmt.setString(2, "TEST");
cstmt.execute();
System.out.println("Statement executed. Now closing down");
System.out.println("Almost done! Getting some more stats if possible");
stats = pds.getStatistics();
System.out.println("\tgetAvailableConnectionsCount() " + stats.getAvailableConnectionsCount());
System.out.println("\tgetBorrowedConnectionsCount() " + stats.getBorrowedConnectionsCount() );
System.out.println("\tgetRemainingPoolCapacityCount() " + stats.getRemainingPoolCapacityCount());
System.out.println("\tgetTotalConnectionsCount() " + stats.getTotalConnectionsCount());
System.out.println(((oracle.ucp.jdbc.oracle.OracleJDBCConnectionPoolStatistics)pds.getStatistics()).getFCFProcessingInfo());
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Closing connection "+ conn);
cstmt.close();
conn.close();
conn = null;
}
}
I got the code from below site and modified a bit to add threads and simulate AC. But I am unable to get a replayable datasource.
https://martincarstenbach.wordpress.com/2013/12/13/playing-with-application-continuity-in-rac-12c/
To test Application Continuity you can kill the sessions on the server by doing ALTER SYSTEM KILL SESSION 'sid,serial#' which will simulate a failure.
Related
We have a standalone java application using third-party tool to manage connection pooling, which worked for us in v6_client + v6_server setup for a long time.
Now we are trying to migrate from v6 to v9 (yes, we are pretty late to the party.....), and found v9_client connection to v6_server connection is constantly interrupted, meaning:
Socket created by XAQueueConnectionFactory#createXAConnection() is always closed immediately, and the created XAConnection seems to be unaware of it.
Due to socket close mentioned above, XASession created from the XAConnection.createXASession() always creates a new socket and close the socket after XASession.close().
We went throught the complete list of tunables for v9_client (XAQCF
column in https://www.ibm.com/support/knowledgecenter/SSFKSJ_9.0.0/com.ibm.mq.ref.dev.doc/q111800_.html) and only spot two potential new configs we haven't used in v6_client, SHARECONVALLOWED and PROVIDERVERSION. Unfortunately neither helps us out..... Specifically:
We tried setShareConvAllowed(WMQConstants.WMQ_SHARE_CONV_ALLOWED_[YES/NO]) Considering there's no SHARECNV property in v6_server side, not a surprise.
We tried "Migration/Restricted/Normal Mode" by setProviderVersion("[6/7/8") ([7/8] throws exceptions, as expected....).
Just wondering if anybody else had similar experience and could share some insights. We tried v9_server+v9_client and haven't seen any similar problem, so that could also be our eventual solution.....
Btw, the WMQ is hosted on linux (RedHat), and we only use products of MQXAQueueConnectionFactory on client side (ibm mq class for jms).
Thanks.
Additional details/updates.
[update-1]
--[playgrond setup]
v9_client jars:
javax.jms-api-2.0.jar
com.ibm.mq.allclient(-9.0.0.[1/5]).jar
v6_client jars:
in addition to v9_client jars, introduced the following jars in eclipse classpath
com.ibm.dhbcore-1.0.jar
com.ibm.mq.pcf-6.0.3.jar
com.ibm.mqjms-6.0.2.2.jar
com.ibm.mq-6.0.2.2.jar
com.ibm.mqetclient-6.0.2.2.jar
connector.jar
jta-1.1.jar
Testing code - single thread:
import javax.jms.*;
import com.ibm.mq.jms.*;
import com.ibm.msg.client.wmq.WMQConstants;
public class MQSeries_simpleAuditQ {
private static String queueManager = "QM.RCTQ.ALL.01";
private static String host = "localhost";
private static int port = 26005;
public static void main(String[] args) throws JMSException {
MQXAQueueConnectionFactory queueFactory= new MQXAQueueConnectionFactory();
System.out.println("\n\n\n*******************\nqueueFactory implementation version: " +
queueFactory.getClass().getPackage().getImplementationVersion() + "*****************\n\n\n");
queueFactory.setHostName(host);
queueFactory.setPort(port);
queueFactory.setQueueManager(queueManager);
queueFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
if (queueFactory.getClass().getPackage().getImplementationVersion().split("\\.")[0].equals("9")) {
queueFactory.setProviderVersion("6");
//queueFactory.setShareConvAllowed(WMQConstants.WMQ_SHARE_CONV_ALLOWED_YES);
}
XASession xaSession;
javax.jms.QueueConnection xaQueueConnection;
try {
// Obtain a QueueConnection
System.out.println("Creating Connection...");
xaQueueConnection = (QueueConnection)queueFactory.createXAConnection(" ", "");
xaQueueConnection.start();
for (int counter=0; counter<2; counter++) {
try {
xaSession = ((XAConnection)xaQueueConnection).createXASession();
xaSession.close();
} catch (Exception ex) {
System.out.println(ex);
}
}
System.out.println("Closing connection.... ");
xaQueueConnection.close();
} catch (Exception e) {
System.out.println("Error processing " + e.getMessage());
}
}
}
--[observations]
v6_client only created and close a single socket, while v9_client (both 9.0.0.[1/5]):
socket created and closed right after xaQueueConnection = (QueueConnection)queueFactory.createXAConnection(" ", "");
in the inner for loop, socket created right after xaSession = ((XAConnection)xaQueueConnection).createXASession();, and closed after xaSession.close();
Naively i was expecting socket remains open until xaQueueConnection.close().
[update-2]
Using queueFactory.setProviderVersion("9"); and queueFactory.setShareConvAllowed(WMQConstants.WMQ_SHARE_CONV_ALLOWED_YES); for v9_server+v9_client, we don't see the same constant socket close issue in v6_server+v9_client, which is a good news.
[update-3] MCAUSER on attribute for all SVRCONN channel on v6_server. Same on v9_server (which doesn't have the same socket close problem when connected with the same v9_client).
display channel (SYSTEM.ADMIN.SVRCONN)
MCAUSER(mqm)
display channel (SYSTEM.AUTO.SVRCONN)
MCAUSER( )
display channel (SYSTEM.DEF.SVRCONN)
MCAUSER( )
[update-4]
I tried setting MCAUSER() to mqm, then using both (blank) and mqm from client side, both can create connections, but still seeing the same unexpected socket close using v9_client+v6_user. After updating MCAUSER(), i always added refresh security, and restart the qmgr.
I also tried setting system variable to blank in eclipse before creating the connection using blank user, didn't help either.
[update-5]
Limiting our discussion to v9_client+v9_server. The async testing code below generates a ton of xasession create/close request, using limited number of existing connections. Using SHARECNV(1) we would also end up with unaffordable high TIME_WAIT count, but using larger than 1 SHARECNV (eg. 10) might introduce extra performance penalty......
Async testing code
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import javax.jms.*;
import com.ibm.mq.jms.*;
import com.ibm.msg.client.wmq.WMQConstants;
public class MQSeries_simpleAuditQ_Async_v9 {
private static String queueManager = "QM.ALPQ.ALL.01";
private static int port = 26010;
private static String host = "localhost";
private static int connCount = 20;
private static int amp = 100;
private static ExecutorService amplifier = Executors.newFixedThreadPool(amp);
public static void main(String[] args) throws JMSException {
MQXAQueueConnectionFactory queueFactory= new MQXAQueueConnectionFactory();
System.out.println("\n\n\n*******************\nqueueFactory implementation version: " +
queueFactory.getClass().getPackage().getImplementationVersion() + "*****************\n\n\n");
queueFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
if (queueFactory.getClass().getPackage().getImplementationVersion().split("\\.")[0].equals("9")) {
queueFactory.setProviderVersion("9");
queueFactory.setShareConvAllowed(WMQConstants.WMQ_SHARE_CONV_ALLOWED_YES);
}
queueFactory.setHostName(host);
queueFactory.setPort(port);
queueFactory.setQueueManager(queueManager);
//queueFactory.setChannel("");
ArrayList<QueueConnection> xaQueueConnections = new ArrayList<QueueConnection>();
try {
// Obtain a QueueConnection
System.out.println("Creating Connection...");
//System.setProperty("user.name", "mqm");
//System.out.println("system username: " + System.getProperty("user.name"));
for (int ct=0; ct<connCount; ct++) {
// xaQueueConnection instance of MQXAQueueConnection
QueueConnection xaQueueConnection = (QueueConnection)queueFactory.createXAConnection(" ", "");
xaQueueConnection.start();
xaQueueConnections.add(xaQueueConnection);
}
ArrayList<Double> totalElapsedTimeRecord = new ArrayList<Double>();
ArrayList<FutureTask<Double>> taskBuffer = new ArrayList<FutureTask<Double>>();
for (int loop=0; loop <= 10; loop++) {
try {
for (int i=0; i<amp; i++) {
int idx = (int)(Math.random()*((connCount)));
System.out.println("Using connection: " + idx);
FutureTask<Double> xaSessionPoker = new FutureTask<Double>(new XASessionPoker(xaQueueConnections.get(idx)));
amplifier.submit(xaSessionPoker);
taskBuffer.add(xaSessionPoker);
}
System.out.println("loop " + loop + " completed");
} catch (Exception ex) {
System.out.println(ex);
}
}
for (FutureTask<Double> xaSessionPoker : taskBuffer) {
totalElapsedTimeRecord.add(xaSessionPoker.get());
}
System.out.println("Average xaSession poking time: " + calcAverage(totalElapsedTimeRecord));
System.out.println("Closing connections.... ");
for (QueueConnection xaQueueConnection : xaQueueConnections) {
xaQueueConnection.close();
}
} catch (Exception e) {
System.out.println("Error processing " + e.getMessage());
}
amplifier.shutdown();
}
private static double calcAverage(ArrayList<Double> myArr) {
double sum = 0;
for (Double val : myArr) {
sum += val;
}
return sum/myArr.size();
}
// create and close session through QueueConnection object passed in.
private static class XASessionPoker implements Callable<Double> {
// conn instance of MQXAQueueConnection. ref. QueueProviderService
private QueueConnection conn;
XASessionPoker(QueueConnection conn) {
this.conn = conn;
}
#Override
public Double call() throws Exception {
XASession xaSession;
double elapsed = 0;
try {
final long start = System.currentTimeMillis();
// ref. DualSessionWrapper
// xaSession instance of MQXAQueueSession
xaSession = ((XAConnection) conn).createXASession();
xaSession.close();
final long end = System.currentTimeMillis();
elapsed = (end - start) / 1000.0;
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e);
}
return elapsed;
}
}
}
We found the root cause is a combination of no more session pooling + bitronix TM doesn't offer session pooling across TX. Specifically (in our case), bitronix manages JmsPooledConnection pooling, but everytime a (xa)session is used (under JmsPooledConnection), a new socket is created (createXASession()) and closed (xaSession.close()).
One solution, is to wrap the jms connection with (xa)session pool, similar to what's been done in https://github.com/messaginghub/pooled-jms/tree/master/pooled-jms/src/main/java/org/messaginghub/pooled/jms
http://bjansen.github.io/java/2018/03/04/high-performance-mq-jms.html also suggests Spring CachingConnectionFactory works well, which sounds like a speical case of the first solution.
I am receiving an error that I did see on your site, but as a novice java coder I think that I flubbed my implementation of the solution.
I am getting a message
"SQL Server version 8 is not supported by this driver. ClientConnectionId:602d619d-c033-41d0-9109-80f56e3ab9b3"
I am using Eclipse Mars 1
The database is a sql server 2000, so I downloaded sqljdbc.jar from the Microsoft site as suggested by an earlier question. I loaded it to c:\temp and added that to the CLASSPATH and rebooted.
code snippet reads:
package texasLAMPConversion;
import java.sql.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TexasLAMPConversion {
public static void main(String[] args) {
// TODO Auto-generated method stub
String host = "jdbc:sqlserver://serverName\\instanceName";
String uName = "*********";
String uPass = "*********";
Connection conn = null;
try {
conn = DriverManager.getConnection(host, uName,uPass);
if (conn != null) { System.out.println("Connected to the database");
}
} catch (SQLException ex) {
System.out.println(ex.getMessage() );
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
FileWriter fw= null;
File file =null;
try {
file=new File("C:/temp/generated_stmts_update_t_customer_master.sql");
if(!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(file);
}catch (IOException e) {
e.printStackTrace();
}
String oldKey = "8000001234", newKey = "8";
int lenAcct = 10;
oldKey = ReformatOldAcct(lenAcct, oldKey );
newKey = leftZeroAcct(lenAcct, newKey);
System.out.println("oldkey = " + oldKey + " and newKey = " + newKey);
}
private static String ReformatOldAcct(int lenAcct, String oldKey) {
// TODO Auto-generated method stub
String reformatedAcct = "";
reformatedAcct = "0" + oldKey.substring(2, lenAcct);
return (reformatedAcct);
}
private static String leftZeroAcct(int lenAcct, String acctNo) {
// TODO Auto-generated method stub
int index =0;
int currentLengthAcct;
String leadingZero = "";
currentLengthAcct = acctNo.length();
while (index < (lenAcct -currentLengthAcct) ) {
leadingZero = '0' + leadingZero;
index ++;
}
return (leadingZero + acctNo);
}
}
and it is throwing this complete message
Apr 19, 2016 11:29:44 AM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
WARNING: ConnectionID:1 ClientConnectionId: 602d619d-c033-41d0-9109-80f56e3ab9b3 Server major version:8 is not supported by this driver.
Apr 19, 2016 11:29:44 AM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
WARNING: ConnectionID:1 ClientConnectionId: 602d619d-c033-41d0-9109-80f56e3ab9b3 Server major version:8 is not supported by this driver.
com.microsoft.sqlserver.jdbc.SQLServerException: SQL Server version 8 is not supported by this driver. ClientConnectionId:602d619d-c033-41d0-9109-80f56e3ab9b3com.microsoft.sqlserver.jdbc.SQLServerException: SQL Server version 8 is not supported by this driver. ClientConnectionId:602d619d-c033-41d0-9109-80f56e3ab9b3
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2226) at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2226)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2210)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2210)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.Prelogin(SQLServerConnection.java:2095)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.Prelogin(SQLServerConnection.java:2095)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1799)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1799)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1454)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1454)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1285)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1285)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1131)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1131)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at texasLAMPConversion.TexasLAMPConversion.main(TexasLAMPConversion.java:20)
at texasLAMPConversion.TexasLAMPConversion.main(TexasLAMPConversion.java:20)
SQL Server version 8 is not supported by this driver. ClientConnectionId:602d619d-c033-41d0-9109-80f56e3ab9b3
SQL Server version 8 is not supported by this driver. ClientConnectionId:602d619d-c033-41d0-9109-80f56e3ab9b3
Any thoughts for a novice java programmer moving away from PowerBuilder?
I have written a piece of code to connect to TIBCO queue and fetch customer data. But I cannot find a way to test it before moving it to production. Can we create a dummy queue somehow so that I can ensure that my program is functional before packaging it for production?
Here is my code..
package GI;
import javax.jms.JMSSecurityException;
import javax.naming.InitialContext;
import javax.jms.Queue;
import javax.jms.TextMessage;
import javax.jms.QueueSender;
import javax.jms.DeliveryMode;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
public class Queue
{
public Queue()
{
System.err.println("\n------------------------------------------------------------------------");
System.err.println("tibjmsTEQueue SAMPLE");
System.err.println("------------------------------------------------------------------------");
}
public String QueueProcess(String XMLRequest, String serverUrl, String userName, String password, String InQueueName)
{
String [] args = new String[4];
args[0]=serverUrl;
args[1]=userName;
args[2]=password;
args[3]=InQueueName;
System.out.println("Server....................... "+serverUrl);
System.out.println("User......................... "+userName);
System.out.println("InQueue...................... "+InQueueName);
System.out.println("------------------------------------------------------------------------\n");
try
{
tibjmsUtilities.initSSLParams(serverUrl,args);
}
catch (JMSSecurityException e)
{
System.err.println("JMSSecurityException: "+e.getMessage()+", provider="+e.getErrorCode());
e.printStackTrace();
System.exit(0);
}
System.err.println("Starting");
QueueConnection connectionIn = null;
try
{
InitialContext context = new InitialContext();
Queue tibcoQueue = (Queue) context.lookup("queue/queue0");
QueueConnectionFactory factory = new com.tibco.tibjms.TibjmsQueueConnectionFactory(serverUrl);
connectionIn = factory.createQueueConnection(userName, password);
QueueSession sessionIn = connectionIn.createQueueSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
QueueSender queueSender = sessionIn.createSender(tibcoQueue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
QueueReceiver queueReceiver = sessionIn.createReceiver(tibcoQueue);
connectionIn.start();
System.out.println("Connected to Queue...");
System.out.println("XMLRequest: " + XMLRequest);
TextMessage sendXMLRequest = sessionIn.createTextMessage(XMLRequest);
queueSender.send(sendXMLRequest);
System.out.println("sent: " + sendXMLRequest.getText());
TextMessage XMLResponse = (TextMessage) queueReceiver.receive();
System.out.println("received: " + XMLResponse.getText());
return((String) XMLResponse.getText());
}
catch(Exception e)
{
System.err.println("Exitting with Error");
e.printStackTrace();
System.exit(0);
}
finally
{
System.err.println("Exitting");
try {connectionIn.close();}
catch(Exception e1) {}
}
return("Failure");
}
public static void main(String args[])
{
Queue t = new Queue();
}
}
It is never suggested to mix production and non-production traffic on the same EMS messaging instance. That is a very risky practice since it introduces the possibility of inadvertently sending non-production traffic to production applications. I would suggest first checking with your EMS operations team as they can likely point you to a development EMS instance that you can use.
For unit and/or integration testing where you are not looking to stress-test the environment, many developers install an instance of EMS on their local development workstation. Again your EMS operations team may be able to provide you with a license and installation binary to install locally.
I'm finding that when multiple threads request a connection each from a single, shared instance of a ComboPooledDataSource, there is the occasion of if returning a connection from the pool that's already in-use. Is there a configuration setting or other means to make sure that connections currently checked-out aren't checked-out again?
package stress;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
public class StressTestDriver
{
private static final String _host = "";
private static final String _port = "3306";
private static final String _database = "";
private static final String _user = "";
private static final String _pass = "";
public static void main(String[] args)
{
new StressTestDriver();
}
StressTestDriver()
{
ComboPooledDataSource cpds = new ComboPooledDataSource();
try
{
cpds.setDriverClass( "com.mysql.jdbc.Driver" );
String connectionString = "jdbc:mysql://" + _host + ":" + _port + "/"
+ _database;
cpds.setJdbcUrl( connectionString );
cpds.setMaxPoolSize( 15 );
cpds.setMaxIdleTime( 100 );
cpds.setAcquireRetryAttempts( 1 );
cpds.setNumHelperThreads( 3 );
cpds.setUser( _user );
cpds.setPassword( _pass );
}
catch( PropertyVetoException e )
{
e.printStackTrace();
return;
}
write("BEGIN");
try
{
for(int i=0; i<100000; ++i)
doConnection( cpds );
}
catch( Exception ex )
{
ex.printStackTrace();
}
finally
{
try
{
DataSources.destroy( cpds );
}
catch( SQLException e )
{
e.printStackTrace();
}
}
write("END");
}
void doConnection( final ComboPooledDataSource cpds )
{
Thread[] threads = new Thread[ 10 ];
final Set<String> set = new HashSet<String>(threads.length);
Runnable runnable = new Runnable()
{
public void run()
{
Connection conn = null;
try
{
conn = cpds.getConnection();
synchronized(set)
{
String toString = conn.toString();
if( set.contains( toString ) )
write("In-use connection: " + toString);
else
set.add( toString );
}
conn.close();
}
catch( Exception e )
{
e.printStackTrace();
return;
}
}
};
for(int i=0; i<threads.length; ++i)
{
threads[i] = new Thread( runnable );
threads[i].start();
}
for(int i=0; i<threads.length; ++i)
{
try
{
threads[i].join();
}
catch( InterruptedException e )
{
e.printStackTrace();
}
}
}
private static void write(String msg)
{
String threadName = Thread.currentThread().getName();
System.err.println(threadName + ": " + msg);
}
}
I've run you stress test in my environment. It terminated without output.
That said, it strikes me as a bit unreliable to use toString() output as a token for identity. The hashcodes encoded in Object.toString() are not guaranteed unique, and you're generating lots. You might just be seeing collisions.
In particular, the scenario you are concerned about would represent a surprising and perplexing sort of bug. You should be seeing instances of com.mchange.v2.c3p0.impl.NewProxyConnection. Those instances are not checked in and out of the pool — they are single use, disposable proxies that wrap the underlying database Connection. They are constructed with new when you call getConnection(), remain associated with a PooledConnection object while you work, then dereferenced by the c3p0 library when you call close(), to be garbage collected after client code dereferences them. If somehow getConnection() was being called on the same underlying PooledConnection, that would be a c3p0 bug, and c3p0 would emit a warning to that effect. You are not seeing anything like...
c3p0 -- Uh oh... getConnection() was called on a PooledConnection when
it had already provided a client with a Connection that has not yet
been closed. This probably indicates a bug in the connection pool!!!
are you?
I'd verify that you are not just seeing collisions of NewProxyConnection.toString(). Use a map rather than a set, hold a reference to close()ed Connection instances as values, when you see a collision of String keys, check with == whether the new Connection is the same instance as the value in your map. I'd be astonished if you find that they are the same instance. (I wish I could reproduce the problem to verify this on my own.)
I added code to get the connection ID/##SPID for Oracle and MS SQL Server, and whenever the toString value was the same, the connection ID always differed, so c3p0 is not the culprit:
94843 - pool-1-thread-12 - : onCheckOut called with: com.microsoft.sqlserver.jdbc.SQLServerConnection#5e65d9 - H: 6186457 - P: : 57
94843 - pool-1-thread-13 - : onCheckOut called with: com.microsoft.sqlserver.jdbc.SQLServerConnection#170a25e - H: 24158814 - P: : 55
94843 - pool-1-thread-1 - : onCheckOut called with: com.microsoft.sqlserver.jdbc.SQLServerConnection#5e65d9 - H: 6186457 - P: : 54
19172 - pool-1-thread-11 - : onCheckOut called with: oracle.jdbc.driver.T4CConnection#2475ca - H: 2389450 - P: : 6564
19172 - pool-1-thread-31 - : onCheckOut called with: oracle.jdbc.driver.T4CConnection#2475ca - H: 2389450 - P: : 6448
I'll have to look into revising the existing code. Thanks for your help Steve! You've pointed me in the right direction.
I'm trying to develop an adf mobile app using jDeveloper and oracle sql developer.
I have connected jDev and sql. I want to populate selectOneChoice comp. that I m gonna fetch datas.
this is my connection method;
package salesorder.application;
import groovy.sql.Sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import oracle.adf.share.jndi.InitialContextFactoryImpl;
import oracle.adfmf.framework.api.AdfmfJavaUtilities;
import oracle.jbo.server.InitialContextImpl;
import oracle.jdbc.connector.OracleConnectionManager;
import oracle.jdbc.pool.OracleDataSource;
public class DBConnection {
public DBConnection() {
super();
}
private static String jdbcUrl = "jdbc:oracle:thin:#10.172.105.37:1521:VIS";
private static String userid = "***";
private static String password = "***";
protected static Connection conn = null;
public static Connection getConnection()throws Exception{
if (conn == null) {
try {
OracleDataSource ds; ds = new OracleDataSource();
ds.setURL(jdbcUrl);
conn=ds.getConnection(userid,password);
} catch (SQLException e) {
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
}
return conn;
}
}
and this, my method to fetch data;
private void Execute() {
Trace.log(Utility.ApplicationLogger, Level.INFO, Customers.class, "Execute",
"!!!!!!!!!!!!!!!!!In COUNTRY Execute!!!!!!!!!!!!!!!!!!!!!!!!!");
try{
Connection conn = DBConnection.getConnection();
customers.clear();
conn.setAutoCommit(false);
PreparedStatement stat= conn.prepareStatement("select cust_account_id,account_name from hz_cust_accounts_all where account_name is not null order by account_name asc");
// fetching customers name
ResultSet rs = stat.executeQuery();
Trace.log(Utility.ApplicationLogger, Level.INFO, Customers.class, "Execute",
"!!!!!!!!!!!!!!!!!Query Executed!!!!!!!!!!!!!!!!!!!!!!!!!");
while(rs.next()){
int id = rs.getInt("CUST_ACCOUNT_ID"); // customer id
String name = rs.getString("ACCOUNT_NAME"); // customer name
Customer c = new Customer(id,name);
customers.add(c);
}
rs.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
when i try to start application, an error comes up like that.
i cant use an image. so
Error
oracle.jdbc.pool.OracleDataSource
I dont know how Im gonna solve that, i cannot figure out why . Any help ?
Just to clarify - are you trying to use ADF Mobile (AMX pages)?
If so then you can't connect with JDBC to a remote database from the client.
You can only connect with JDBC to the local SQLite DB on your device.
To access data from remote servers you'll need to expose this data with web services and call those.