Skip to content

unable to save mbcs characters in step execution context [BATCH-2850] #764

@spring-projects-issues

Description

@spring-projects-issues

shreyas deshpande opened BATCH-2850 and commented

i am using spring batch with Jdbc and Postgres DB. all the job and step execution context data gets saved in spring batch created tables in PostgreS DB.

i am using it to save some step context data, which gets saved in batch_step_execution_context table in the column SERIALIZED_CONTEXT. the data i am saving has some MBCS characters.

but i see that when writing data to the table and reading from it its using ISO-8859-1 charset. hence my mbcs characters though serialized by Xstream default serializer gets stored as garbage.

any way to workaround this, so i can retrieve and save data as MBCS.

please find the code snippet from JDBCExecutionContextDao.

private String serializeContext(ExecutionContext ctx) {
	Map<String, Object> m = new HashMap<>();
	for (Entry<String, Object> me : ctx.entrySet()) {
		m.put(me.getKey(), me.getValue());
	}

	ByteArrayOutputStream out = new ByteArrayOutputStream();
	String results = "";

	try {
		serializer.serialize(m, out);
		results = new String(out.toByteArray(), "ISO-8859-1");
	}
	catch (IOException ioe) {
		throw new IllegalArgumentException("Could not serialize the execution context", ioe);
	}

	return results;
}

private class ExecutionContextRowMapper implements RowMapper<ExecutionContext> {

	@Override
	public ExecutionContext mapRow(ResultSet rs, int i) throws SQLException {
		ExecutionContext executionContext = new ExecutionContext();
		String serializedContext = rs.getString("SERIALIZED_CONTEXT");
		if (serializedContext == null) {
			serializedContext = rs.getString("SHORT_CONTEXT");
		}

		Map<String, Object> map;
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(serializedContext.getBytes("ISO-8859-1"));
			map = serializer.deserialize(in);
		}
		catch (IOException ioe) {
			throw new IllegalArgumentException("Unable to deserialize the execution context", ioe);
		}
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			executionContext.put(entry.getKey(), entry.getValue());
		}
		return executionContext;
	}
}

i expect to store and retrieve mbcs data.

on some suggestions on stackoverflow tried Jackson2ExecutionContextStringSerializer along with XStreamExecutionContextStringSerializer . but the issue exists.

as a workaround i myseld converted the object to base64 encoded string and then saved it to step execution context. but would like spring batch to take care of this.


Affects: 3.0.10

Reference URL: https://stackoverflow.com/questions/56599638/mbcs-data-in-stepexecution-in-spring-batch-database/56600147#56600147

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions