Do not leak database connections

This commit is contained in:
Zsolt Parragi 2023-04-12 14:02:28 +02:00 committed by arp102
parent 1a0f63af61
commit 5f34baee0d
2 changed files with 67 additions and 66 deletions

View File

@ -355,80 +355,81 @@ class KmipEngine(object):
def _process_batch(self, request_batch, batch_handling, batch_order): def _process_batch(self, request_batch, batch_handling, batch_order):
response_batch = list() response_batch = list()
self._data_session = self._data_store_session_factory() with self._data_store_session_factory() as session:
self._data_session = session
for batch_item in request_batch: for batch_item in request_batch:
error_occurred = False error_occurred = False
response_payload = None response_payload = None
result_status = None result_status = None
result_reason = None result_reason = None
result_message = None result_message = None
operation = batch_item.operation operation = batch_item.operation
request_payload = batch_item.request_payload request_payload = batch_item.request_payload
# Process batch item ID. # Process batch item ID.
if len(request_batch) > 1: if len(request_batch) > 1:
if not batch_item.unique_batch_item_id: if not batch_item.unique_batch_item_id:
raise exceptions.InvalidMessage( raise exceptions.InvalidMessage(
"Batch item ID is undefined." "Batch item ID is undefined."
)
# Process batch message extension.
# TODO (peterhamilton) Add support for message extension handling.
# 1. Extract the vendor identification and criticality indicator.
# 2. If the indicator is True, raise an error.
# 3. If the indicator is False, ignore the extension.
# Process batch payload.
try:
response_payload = self._process_operation(
operation.value,
request_payload
) )
# Process batch message extension. result_status = enums.ResultStatus.SUCCESS
# TODO (peterhamilton) Add support for message extension handling. except exceptions.KmipError as e:
# 1. Extract the vendor identification and criticality indicator. error_occurred = True
# 2. If the indicator is True, raise an error. result_status = e.status
# 3. If the indicator is False, ignore the extension. result_reason = e.reason
result_message = str(e)
except Exception as e:
self._logger.warning(
"Error occurred while processing operation."
)
self._logger.exception(e)
# Process batch payload. error_occurred = True
try: result_status = enums.ResultStatus.OPERATION_FAILED
response_payload = self._process_operation( result_reason = enums.ResultReason.GENERAL_FAILURE
operation.value, result_message = (
request_payload "Operation failed. See the server logs for more "
"information."
)
# Compose operation result.
result_status = contents.ResultStatus(result_status)
if result_reason:
result_reason = contents.ResultReason(result_reason)
if result_message:
result_message = contents.ResultMessage(result_message)
batch_item = messages.ResponseBatchItem(
operation=batch_item.operation,
unique_batch_item_id=batch_item.unique_batch_item_id,
result_status=result_status,
result_reason=result_reason,
result_message=result_message,
response_payload=response_payload
) )
response_batch.append(batch_item)
result_status = enums.ResultStatus.SUCCESS # Handle batch error if necessary.
except exceptions.KmipError as e: if error_occurred:
error_occurred = True if batch_handling == enums.BatchErrorContinuationOption.STOP:
result_status = e.status break
result_reason = e.reason
result_message = str(e)
except Exception as e:
self._logger.warning(
"Error occurred while processing operation."
)
self._logger.exception(e)
error_occurred = True
result_status = enums.ResultStatus.OPERATION_FAILED
result_reason = enums.ResultReason.GENERAL_FAILURE
result_message = (
"Operation failed. See the server logs for more "
"information."
)
# Compose operation result.
result_status = contents.ResultStatus(result_status)
if result_reason:
result_reason = contents.ResultReason(result_reason)
if result_message:
result_message = contents.ResultMessage(result_message)
batch_item = messages.ResponseBatchItem(
operation=batch_item.operation,
unique_batch_item_id=batch_item.unique_batch_item_id,
result_status=result_status,
result_reason=result_reason,
result_message=result_message,
response_payload=response_payload
)
response_batch.append(batch_item)
# Handle batch error if necessary.
if error_occurred:
if batch_handling == enums.BatchErrorContinuationOption.STOP:
break
return response_batch return response_batch

View File

@ -13,4 +13,4 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
__version__ = "0.11.0dev1" __version__ = "0.11.0.dev1"