dbunit 在系统应用(文件导入和导出)中的资源释放问题

我在应用中使用dbunit做文件的导入和导出功能,现在导入和导出功能都能很好的实现,但是有一个资源释放的问题,一直都没找着原因,现把代码贴出来,望同志们发表高见。
public abstract class BaseImportor extends AbstractStep implements Importor {
    private final static Log log = LogFactory.getLog(BaseImportor.class);
    
    private static final String batchID = "http://www.dbunit.org/features/batchedStatements";
    private static DataSource ds;
    
    protected IDataSet srcDataSet;

    protected String _type = TYPE_CLEAN_INSERT;
    private boolean _transaction = false;
    private DatabaseOperation _operation;
    protected boolean _forwardOperation = true;
    
    /**
     * 通过spring注射dataSource
     * @param dataSource
     */
    public final void setDataSource(final DataSource dataSource){
    	ds = dataSource;
    }
    public BaseImportor() {
        
    }
    /**
     * 获取连接
     * @return
     * @throws DatabaseUnitException
     */
    protected IDatabaseConnection getConnection() throws DatabaseUnitException{
    	IDatabaseConnection iDataBaseConnection = null;
		try {
			String schema = ds.getConnection().getMetaData().getUserName();
			iDataBaseConnection = new DatabaseConnection(ds.getConnection(), schema);
			DatabaseConfig config = iDataBaseConnection.getConfig();
			if (!config.getFeature(batchID)) {
			    config.setFeature(batchID, true);
			}
			log.debug("get database connection with schema '"+schema+"'");
		} catch (SQLException e) {
			log.error(e);
			throw new DatabaseUnitException(e);
		}
        return iDataBaseConnection;
    }

    public String getType()
    {
        return _type;
    }

    public DatabaseOperation getDbOperation()
    {
        return _operation;
    }

    public boolean isTransaction()
    {
        return _transaction;
    }
    /**
     * 设置操作类型
     */
    public void setType(String type)
    {
        if (TYPE_UPDATE.equals(type))
        {
            _operation = DatabaseOperation.UPDATE;
            _forwardOperation = true;
        }
        else if (TYPE_INSERT.equals(type))
        {
            _operation = DatabaseOperation.INSERT;
            _forwardOperation = true;
        }
        else if (TYPE_REFRESH.equals(type))
        {
            _operation = DatabaseOperation.REFRESH;
            _forwardOperation = true;
        }
        else if (TYPE_DELETE.equals(type))
        {
            _operation = DatabaseOperation.DELETE;
            _forwardOperation = false;
        }
        else if (TYPE_DELETE_ALL.equals(type))
        {
            _operation = DatabaseOperation.DELETE_ALL;
            _forwardOperation = false;
        }
        else if (TYPE_CLEAN_INSERT.equals(type))
        {
            _operation = DatabaseOperation.CLEAN_INSERT;
            _forwardOperation = false;
        }
        else if (TYPE_NONE.equals(type))
        {
            _operation = DatabaseOperation.NONE;
            _forwardOperation = true;
        }
        else if (TYPE_MSSQL_CLEAN_INSERT.equals(type))
        {
            _operation = InsertIdentityOperation.CLEAN_INSERT;
            _forwardOperation = false;
        }
        else if (TYPE_MSSQL_INSERT.equals(type))
        {
            _operation = InsertIdentityOperation.INSERT;
            _forwardOperation = true;
        }
        else if (TYPE_MSSQL_REFRESH.equals(type))
        {
            _operation = InsertIdentityOperation.REFRESH;
            _forwardOperation = true;
        }
        else
        {
            throw new IllegalArgumentException("Type must be one of: UPDATE, INSERT,"
                    + " REFRESH, DELETE, DELETE_ALL, CLEAN_INSERT, MSSQL_INSERT, "
                    + " or MSSQL_REFRESH but was: " + type);
        }
        _type = type;
    }


    public void setTransaction(boolean transaction)
    {
        _transaction = transaction;
    }

    public void execute(IDatabaseConnection connection) throws DatabaseUnitException
    {
        if (_operation == null)
        {
            throw new DatabaseUnitException("Inputor.execute(): setType(String) must be called before execute()!");
        }

        if (_operation == DatabaseOperation.NONE)
        {
            return;
        }

        try
        {
        	DatabaseOperation operation = (_transaction ? new TransactionOperation(_operation) : _operation);
            operation.execute(connection, getSrcDataSet());
        }
        catch (SQLException e)
        {
            throw new DatabaseUnitException(e);
        }finally{
			try {
				connection.close();
			} catch (Exception e) {	}
			finally{connection = null;}//显示关闭连接
		}
    }

    public String getLogMessage()
    {
        return "";
    }
    
    public abstract IDataSet getSrcDataSet();
    



}

public class ExcelImportor extends BaseImportor{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ExcelImportor.class);

	private InputStream inputStream;
	
	/* (非 Javadoc)
	 * @see com.harmony.ioput.BaseInputor#getSrcDataSet()
	 */
	@Override
	public IDataSet getSrcDataSet(){
		// TODO
		IDataSet dataSet = null;
		try {
			if(inputStream != null){
				dataSet = new XlsDataSet(inputStream);

			}else{
				throw new IllegalArgumentException("请在file和inputStream中选择一种数据源");
			}
		} catch (DataSetException e) {
			logger.error(e);
			throw new DatabaseUnitRuntimeException(e);
		} catch (IOException e) {
			logger.error(e);
			throw new RuntimeException(e);
		}finally{
			try{//释放资源
				inputStream.close();
			}catch(Exception e){}finally{
				inputStream = null;
			}
		}
		return dataSet;
	}

	/* (非 Javadoc)
	 * @see com.harmony.ioput.Inputor#setFile(java.io.File)
	 */
	public void setFile(File src) {
		try {
			this.inputStream = new FileInputStream(src);
		} catch (FileNotFoundException e) {
			throw new IllegalArgumentException(e);
		}

	}

	/* (非 Javadoc)
	 * @see com.harmony.ioput.Inputor#setInputStream(java.io.InputStream)
	 */
	public void setInputStream(InputStream in) {
		this.inputStream = in;
	}

	/**
	 * @return inputStream
	 */
	public InputStream getInputStream() {
		return inputStream;
	}
	/**
	 * 执行导入操作
	 */
	public void execute() {
		try {
			super.execute(getConnection());
		} catch (DatabaseUnitException e) {
			logger.error(e);
			throw new DatabaseUnitRuntimeException(e);
		}
		
	}

}

在代码中,我都有显示的释放connection和关闭inputstrean,但是为什么程序运行到第8次的时候不再运行了,就像是死锁了。

你可能感兴趣的:(spring,编程)