Skip to content

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

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

Closed
spring-projects-issues opened this issue Oct 18, 2019 · 1 comment
Labels
in: core status: superseded Issues that are superseded by other issues type: bug

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 18, 2019

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

@fmbenhassine
Copy link
Contributor

The default encoding in JdbcExecutionContextDao has been changed to UTF-8 (see #3983), which should fix this issue. If not, please re-open it and provide a minimal complete example that reproduces the problem.

@fmbenhassine fmbenhassine added the status: superseded Issues that are superseded by other issues label May 17, 2022
@fmbenhassine fmbenhassine removed this from the 5.0.0 milestone May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core status: superseded Issues that are superseded by other issues type: bug
Projects
None yet
Development

No branches or pull requests

2 participants