`
xiaoxiong_it
  • 浏览: 130486 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

NonTransientConnectionException: No operations allowed after connection closed

 
阅读更多

最近做项目,要用到两个库里的两个表。

自己在用JDBC对另一个库中的表进行操作的时候,第一次修改密码很成功(两个表中的数据都进行了修改),然而当我再次修改密码的时候,却出现:

NonTransientConnectionException: No operations allowed after connection closed

这样的错误,好几天都不知道怎么解决。代码如下:

/**
 * modify cloudStack user password(JDBC)
 * @author xxxx
 * time:2012-03-13
 * updateTime:2012-03-19
 */
@Repository
public class CloudStackUserDAO implements ICloudStackUserDAO{
	
	private static final Log logger = LogFactory.getLog(CloudStackUserDAO.class);
	
	//get jdbc connection
        //DBUtil 获得 connection
        private static Connection conn = DBUtil.conn();

	private static Statement stmt = null;
	
	public CloudStackUserDAO() {
		// do nothing
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cloud.dao.cloudstack.ICloudStackUserDAO#modifyCloudStackUserPassword(long, java.lang.String, java.lang.String)
	 */
	@Override
	public void modifyCloudStackUserPassword(long cloudUserId, String userName, String password){
		
		logger.debug("modify cloudStack user password");
		
		/*try {
			// 取消掉自动提交
			//conn.setAutoCommit(false);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}*/
		
		
		
		String sql = "update cloud.`user` u set password='" + password + "' where u.account_id=" 
			+ cloudUserId + " and u.username='" + userName +"'";
		logger.info("modify cloudStack user password sql: " + sql);
		try {
			stmt = conn.createStatement();
			int i = stmt.executeUpdate(sql);
			//conn.commit(); // commit  
			logger.info("modify cloudStack user password success , update rows = " + i);
		} catch (SQLException e) {
			logger.info("modify cloudStack user password failed");
			e.printStackTrace();
			/*try {
				//conn.rollback(); //rollback
			} catch (SQLException e1) {
				e1.printStackTrace();
			}*/
		}finally{
			try {
				if(stmt != null) {
					stmt.close();
				}
				if(conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

 当第二次进行操作的时候就会出现上面所说的问题。

后来经过尝试对代码做了相应的修改,如下:

/**
 * modify cloudStack user password(JDBC)
 * @author wanggang
 * time:2012-03-13
 * updateTime:2012-03-19
 */
@Repository
public class CloudStackUserDAO implements ICloudStackUserDAO{
	
	private static final Log logger = LogFactory.getLog(CloudStackUserDAO.class);
	
	//get jdbc connection 在这里做了相应的修改
	private static Connection conn = null;
	private static Statement stmt = null;
	
	public CloudStackUserDAO() {
		// do nothing
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cloud.dao.cloudstack.ICloudStackUserDAO#modifyCloudStackUserPassword(long, java.lang.String, java.lang.String)
	 */
	@Override
	public void modifyCloudStackUserPassword(long cloudUserId, String userName, String password){
		
		logger.debug("modify cloudStack user password");
		
		/*try {
			// 取消掉自动提交
			//conn.setAutoCommit(false);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}*/
		
		conn = DBUtil.conn();
		
		String sql = "update cloud.`user` u set password='" + password + "' where u.account_id=" 
			+ cloudUserId + " and u.username='" + userName +"'";
		logger.info("modify cloudStack user password sql: " + sql);
		try {
			stmt = conn.createStatement();
			int i = stmt.executeUpdate(sql);
			//conn.commit(); // commit  
			logger.info("modify cloudStack user password success , update rows = " + i);
		} catch (SQLException e) {
			logger.info("modify cloudStack user password failed");
			e.printStackTrace();
			/*try {
				//conn.rollback(); //rollback
			} catch (SQLException e1) {
				e1.printStackTrace();
			}*/
		}finally{
			try {
				if(stmt != null) {
					stmt.close();
				}
				if(conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

 至于为什么这样做,我也在思考中,希望大家给予指点。

分享到:
评论
1 楼 freerambo 2013-11-07  
我也遇到这个问题了,主要是数据库线程池的空闲线程关闭了,你需要配置空闲时间,还有是否测试

		<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
			<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
			
			<property name="validationQuery" value="${validationQuery}" />
			<property name="testWhileIdle" value="${testWhileIdle}" />
			<property name="testOnBorrow" value="${testOnBorrow}" />


#set to 'SELECT 1'
validationQuery = SELECT 1
#set to 'true'
testWhileIdle = true
#some positive integer
timeBetweenEvictionRunsMillis = 3600000
#set to something smaller than 'wait_timeout'
minEvictableIdleTimeMillis = 18000000
#if you don't mind a hit for every getConnection(), set to "true"
testOnBorrow=true

相关推荐

    cp30报错No operations allowed after connection closed

    使用mysql+cp30连接池时,报错No operations allowed after connection closed。从报错信息来看,是connection断开导致的错误。在网上搜索后发现,较新版本的mysql配置了connection的默认时间,默认时间一般为8个...

    BURNINTEST--硬件检测工具

    - Video playback, Hard disk and CD/DVD test 'no operations' error reporting changed. - When BurnInTest crashes, it will not generate a "minidump" file. Minidumps will need to be sent to Microsoft ...

    微软内部资料-SQL性能优化3

    If no rows satisfy the WHERE condition the first time the range is scanned, no rows should be returned on any subsequent scans. Key range locks are similar to row locks on index keys (whether ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: After deleting the selected points in the TFlexPanel.DeleteSelectedPoints the all figures with one point also deleted. - FIX: When the curve contain more then one figure and they were all ...

    微软内部资料-SQL性能优化2

    To reserve or commit memory and unintentionally not release it when it is no longer being used. A process can leak resources such as process memory, pool memory, user and GDI objects, handles, threads...

    a project model for the FreeBSD Project.7z

    After providing definitions of terms used, this document will outline the organisational structure (including role descriptions and communication lines), discuss the methodology model and after ...

Global site tag (gtag.js) - Google Analytics