I am trying to insert into Oracle database which has two columns-
ID Primary Key varchar2 (4000)
ACCOUNT varchar2 (4000)
I wrote a Multithreaded program for that. And each thread is using unique id
every time to insert into ID column
as ID
is primary key.
The only problem that I am facing at some point is- The below code, throws following exception after running for few seconds.
1) Null Pointer Exception
2) java.sql.SQLException: Listener refused the connection with the following error:ORA-12519, TNS:no appropriate service handler found
I am not able to find any root cause of this problem in my code as everything is looking good to me. As I am closing each and every connection properly. Then how does this NPE is getting thrown and other exception as well?
ExecutorService service = Executors.newFixedThreadPool(10);
try {
// queue some tasks
for (int i = 0; i < 100 * 10; i++) {
service.submit(new ThreadTask());
}
service.shutdown();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
while (!service.isTerminated()) {
}
} catch (InterruptedException e) {
LOG.warn("Threw a Interrupted Exception in" + XMPLoadTest.class.getSimpleName()
+ ".XMPLoadTest: boss told me to stop...Not my fault!!");
}
Below is ThreadTask class-
class ThreadTask implements Runnable {
private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String CONNECTION = "jdbc:oracle:thin:@localhost:1521:ORCL";
private static final String USER = "scott";
private static final String PASSWORD = "tiger";
private static Connection dbConnection = null;
private static PreparedStatement preparedStatement = null;
private static final AtomicInteger id = new AtomicInteger(1);
private final static Logger LOG = Logger.getLogger(ThreadTask.class.getName());
public ThreadTask() {
}
@Override
public void run() {
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(Constants.INSERT_ORACLE_SQL);
preparedStatement.setString(1, String.valueOf(id.getAndIncrement()));
preparedStatement.setString(2, Constants.A_ACCOUNT);
preparedStatement.executeUpdate();
} catch (Exception e) {
// NPE getting thrown here/And second exception as well
LOG.error("Threw a SQLException in " + getClass().getSimpleName(), e);
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
preparedStatement = null;
} catch (SQLException e) {
//Oouch...
LOG.error("Threw a SQLException in finally block of prepared statement " + getClass().getSimpleName(), e);
}
}
if (dbConnection != null) {
try {
dbConnection.close();
dbConnection = null;
} catch (SQLException e) {
//Better go and look for SQL.
LOG.error("Threw a SQLException in finally block of dbConnection " + getClass().getSimpleName(), e);
}
}
}
}
/**
* Attempts to establish a connection to the given database URL
*
* @return the db connection
*/
private Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(XMP_DRIVER);
dbConnection = DriverManager.getConnection(CONNECTION, USER, PASSWORD);
} catch (ClassNotFoundException e) {
LOG.error("Threw a ClassNotFoundException in " + getClass().getSimpleName(), e);
} catch (SQLException e) {
//DAMN! I'm not....
LOG.error("Threw a SQLException in " + getClass().getSimpleName(), e);
} catch (Exception e) {
LOG.error("Threw a Exception in " + getClass().getSimpleName(), e);
}
return dbConnection;
}
}
Is there any potential problem here with my code? I am more worried about this NPE.
StackTrace:
19:14:28,372 ERROR ThreadTask:187 - Threw a SQLException in ThreadTask
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:458)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(DriverManager.java:322)
at java.sql.DriverManager.getConnection(DriverManager.java:358)
at com.ebay.xmp.lnp.ThreadTask.getDBConnection(XMPLoadTest.java:179)
at com.ebay.xmp.lnp.ThreadTask.run(XMPLoadTest.java:137)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
at java.util.concurrent.FutureTask.run(FutureTask.java:149)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
at java.lang.Thread.run(Thread.java:736)
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:395)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1102)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320)
... 14 more
19:14:28,376 ERROR ThreadTask:139 - Threw a SQLException in ThreadTask
java.lang.NullPointerException
at com.ebay.xmp.lnp.ThreadTask.run(XMPLoadTest.java:137)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
at java.util.concurrent.FutureTask.run(FutureTask.java:149)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
at java.lang.Thread.run(Thread.java:736)
The race condition is the simplest explanation because in the current code there is guarantee for the dbConnection
to exists as non-null value when multiple threads are running.
To simply put, every ThreadTask
object has access to the static Connection dbConnection
class field. If for evaluation we take that there are 3 threads started during the applications initiation, and the following happens:
Main+----------------------------------------------------------------------(T)
|+Thread1 --dbConnection=getDBConnection()-------------dbConnection=null|
| |
|----------+Thread2 --dbConnection=getDBConnection()--------------------|dbConnection=null
|
|-------------+Thread3--dbConnection=getDBConnection()------------------|preparedStatement.executeUpdate()
At time (T), since the static variable is set to null
by the Thread1
, your Thread3
will throw up the exception.
* Update *
You need to make use of connection pooling, check out c3p0
* End Update *