Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.5 ~ 3.7] JdbcTemplate #190

Closed
chldbtjd2272 opened this issue Dec 2, 2020 · 4 comments
Closed

[3.5 ~ 3.7] JdbcTemplate #190

chldbtjd2272 opened this issue Dec 2, 2020 · 4 comments
Assignees
Labels
Spring of Toby 3.1 토비의 스프링 3.1

Comments

@chldbtjd2272
Copy link
Contributor

p.[페이지] 3.6~3.7

질문 : jdbcTemplate에 대한 예제로 update~query까지 template/callback 패턴이 어떤식으로 적용되었는지 정리해주면 좋을듯.
나중에 이 패턴들 보고 같이 얘기해보면 구현 수준도 많이 높아질거 같아

@chldbtjd2272 chldbtjd2272 added the Spring of Toby 3.1 토비의 스프링 3.1 label Dec 2, 2020
@csbsjy
Copy link
Contributor

csbsjy commented Dec 3, 2020

요건 블로그 글 작성할 때 정리할게

@csbsjy
Copy link
Contributor

csbsjy commented Dec 3, 2020

@Override
	@Nullable
	public <T> T query(final String sql, final ResultSetExtractor<T> rse) throws DataAccessException {
		Assert.notNull(sql, "SQL must not be null");
		Assert.notNull(rse, "ResultSetExtractor must not be null");
		if (logger.isDebugEnabled()) {
			logger.debug("Executing SQL query [" + sql + "]");
		}

		/**
		 * Callback to execute the query.
		 */
		class QueryStatementCallback implements StatementCallback<T>, SqlProvider {
			@Override
			@Nullable
			public T doInStatement(Statement stmt) throws SQLException {
				ResultSet rs = null;
				try {
					rs = stmt.executeQuery(sql);
					return rse.extractData(rs);
				}
				finally {
					JdbcUtils.closeResultSet(rs);
				}
			}
			@Override
			public String getSql() {
				return sql;
			}
		}

		return execute(new QueryStatementCallback());
	}
	@Override
	@Nullable
	public <T> T execute(StatementCallback<T> action) throws DataAccessException {
		Assert.notNull(action, "Callback object must not be null");

		Connection con = DataSourceUtils.getConnection(obtainDataSource());
		Statement stmt = null;
		try {
			stmt = con.createStatement();
			applyStatementSettings(stmt);
			T result = action.doInStatement(stmt);
			handleWarnings(stmt);
			return result;
		}
		catch (SQLException ex) {
			// Release Connection early, to avoid potential connection pool deadlock
			// in the case when the exception translator hasn't been initialized yet.
			String sql = getSql(action);
			JdbcUtils.closeStatement(stmt);
			stmt = null;
			DataSourceUtils.releaseConnection(con, getDataSource());
			con = null;
			throw translateException("StatementCallback", sql, ex);
		}
		finally {
			JdbcUtils.closeStatement(stmt);
			DataSourceUtils.releaseConnection(con, getDataSource());
		}
	}

@csbsjy
Copy link
Contributor

csbsjy commented Dec 5, 2020

제네릭. jdbc.query 의 내부클래스

@csbsjy csbsjy closed this as completed Dec 5, 2020
@csbsjy
Copy link
Contributor

csbsjy commented Dec 13, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Spring of Toby 3.1 토비의 스프링 3.1
Development

No branches or pull requests

2 participants