Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat could be the possibilities of using a thread-unsafe java.sql.Connection object in java?
    primarykey
    data
    text
    <p>I am dealing with a legacy code where there the connection object in the singleton dao class is a member variable and is prone to race-conditions.</p> <p>I know this is a potential design issue however I am interested in knowing about the different types of problems that could be thought of when dealing with the jdbc connection object in java.</p> <p>Following is the EventLoggerDAO class code:</p> <pre><code>package com.code.ref.dao; import java.sql.Connection; import java.sql.PreparedStatement; import com.code.ref.utils.common.DBUtil; import com.code.ref.utils.common.PCMLLogger; public class EventLoggerDAO { private static EventLoggerDAO staticobj_EventLoggerDAO; private Connection obj_ClsConnection; private PreparedStatement obj_ClsPreparedStmt; private EventLoggerDAO() { try { obj_ClsConnection = DBUtil.getConnection(); } catch (Exception e) { PCMLLogger.logMessage(EventLoggerDAO.class, "EventLoggerDAO()", "Some problem in creating db connection:" + e); } } public static synchronized EventLoggerDAO getInstance() { if (staticobj_EventLoggerDAO == null) { synchronized (EventLoggerDAO.class) { if (staticobj_EventLoggerDAO == null) staticobj_EventLoggerDAO = new EventLoggerDAO(); } } return staticobj_EventLoggerDAO; } public void addEvent(String sName, String sType, String sAction, String sModifiedBy) throws Exception { StringBuffer sbQuery = new StringBuffer(); sbQuery.append("INSERT INTO TM_EVENT_LOG (NAME, TYPE, ACTION, MODIFIED_BY) ").append("VALUES (?, ?, ?, ?) "); if(obj_ClsConnection == null) obj_ClsConnection = DBUtil.getConnection(); obj_ClsPreparedStmt = obj_ClsConnection.prepareStatement(sbQuery.toString()); obj_ClsPreparedStmt.setString(1, sName); obj_ClsPreparedStmt.setString(2, sType); obj_ClsPreparedStmt.setString(3, sAction); obj_ClsPreparedStmt.setString(4, sModifiedBy); obj_ClsPreparedStmt.executeUpdate(); if (obj_ClsPreparedStmt != null) { obj_ClsPreparedStmt.close(); obj_ClsPreparedStmt = null; } } } </code></pre> <p><strong>Problem observed</strong>:</p> <p>Sometimes it happens that the table <code>TM_EVENT_LOG</code> stops inserting and there is not even exception in the server logs.</p> <p>I suspect that during race conditions the connection objects held by different threads might be leading to inconsistent state and might also not be commiting the data. The connection is derived through a websphere datasource maintaining a connection pool.</p> <p>Any thoughts or ideas why this might be happening?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload