commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "jerrybi (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (POOL-230) waiting on <0x00000000e7440168> (a org.apache.commons.pool.impl.GenericObjectPool$Latch)
Date Tue, 25 Sep 2012 01:58:07 GMT

    [ https://issues.apache.org/jira/browse/POOL-230?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13462356#comment-13462356
] 

jerrybi commented on POOL-230:
------------------------------

2. ( or 3.)  does not hold the connections. it just wait the database operation to complete.


it is how I use connections

DBPool is a wrapper of BasicDataSource, you can just think it is BasicDataSource


public class ConnectionManager {
	private static final Logger logger = Logger.getLogger(ConnectionManager.class);
	private static ThreadLocal<Connection> connThreadLocal = new ThreadLocal<Connection>();
	private static DBPool pool;

	public static void init(DBPool pool) {
		ConnectionManager.pool = pool;
	}
	
	public static void startTransaction() throws SQLException {
		if (pool == null) {
			throw new RuntimeException("FATAL:ConnectionManager is not inited!");
		}

		Connection conn = connThreadLocal.get();
		if (conn != null) {
			throw new RuntimeException("FATEL:Transaction Hang");
		}

		conn = pool.getConn();
		conn.setAutoCommit(false);
		connThreadLocal.set(conn);
	}
	
	public static void commit() throws SQLException {
		Connection conn = connThreadLocal.get();
		if (conn == null) {
			logger.warn("commit with null conn!");
			return;
		}
		connThreadLocal.set(null);
		try {
			try {
				if (conn.getAutoCommit()) {
					logger.warn("commit with autoCommit!");
				}
			} catch (SQLException e) {
				logger.error("getAutoCommit failed!", e);
			}
			conn.commit();
		} catch (SQLException e) {
			logger.error("commit failed!", e);
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e) {
				logger.error("setAutoCommit(true) failed!", e);
			}
			closeConn(conn);
		}
	}
	
	public static void rollBack() {
		Connection conn = connThreadLocal.get();
		if (conn == null)
			return;
		
		connThreadLocal.set(null);

		try {
			conn.rollback();
		} catch (Exception e) {
			// ignore
			logger.error("rollback failed!", e);
		}
		try {
			conn.setAutoCommit(true);
		} catch (Exception e) {
			logger.error("setAutoCommit(true) failed!", e);
		}
		closeConn(conn);
	}
	
	public static Connection getConnection() throws SQLException {
		if (pool == null) {
			throw new RuntimeException("FATAL:ConnectionManager is not inited!");
		}
		Connection conn = connThreadLocal.get();
		if (conn == null)
			conn = pool.getConn();
		return conn;
	}
	
	public static void releaseConnection(Connection conn) {
		if (pool == null) {
			throw new RuntimeException("FATAL:ConnectionManager is not inited!");
		}
		if (connThreadLocal.get() == null) {
			closeConn(conn);
		}
	}
	
	public static void closePreparedStatement(PreparedStatement ps) {
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				logger.error("close ps error", e);
			}
		}
	}
	
	public static void closeResultSet(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				logger.error("close rs error", e);
			}
		}
	}
	
	private static void closeConn(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("close conn error", e);
			}
		}
	}
}


I use Connection like this:

1. use Transaction

funtion() {

    try {
    	ConnectionManager.startTransaction();
   	Connection = null;

    	part1();
   	part2();
     

    	ConnectionManager.commit();    
    }
    catch(Exception e) {
        ConnectionManager.rollback();
    } 

}



part1(), part2()  like:

    try {
        conn = ConnectionManager.getConnection();
        
        do something
        
    }finally {
        ConnectionManager.closeResultSet(rs);
        ConnectionManager.closePreparedStatement(ps);
        ConnectionManager.releaseConnection(conn);
    }


2. not use Transaction

function() {
    Connection = null;
  
    try {
        conn = ConnectionManager.getConnection();
        
        do something
        
    }finally {
        ConnectionManager.closeResultSet(rs);
        ConnectionManager.closePreparedStatement(ps);
        ConnectionManager.releaseConnection(conn);
    }

}

     
                
> waiting on <0x00000000e7440168> (a org.apache.commons.pool.impl.GenericObjectPool$Latch)
> ----------------------------------------------------------------------------------------
>
>                 Key: POOL-230
>                 URL: https://issues.apache.org/jira/browse/POOL-230
>             Project: Commons Pool
>          Issue Type: Bug
>    Affects Versions: 1.5.4
>         Environment: tomcat using dbcp
>            Reporter: jerrybi
>
> "Thread-20" daemon prio=10 tid=0x00007f5810214000 nid=0x5f7b in Object.wait() [0x00007f581527c000]
>    java.lang.Thread.State: TIMED_WAITING (on object monitor)
> 	at java.lang.Object.wait(Native Method)
> 	- waiting on <0x00000000e7440168> (a org.apache.commons.pool.impl.GenericObjectPool$Latch)
> 	at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1112)
> 	- locked <0x00000000e7440168> (a org.apache.commons.pool.impl.GenericObjectPool$Latch)
> 	at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
> 	at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
> it seems that the problem bellow still exits. 
> https://issues.apache.org/jira/browse/POOL-149
> I am using commons-dbcp-1.3 and commons-pool-1.5.4

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message