我使用的是最新的Postgres版本,但如果我调用registerSQLType(paramName,Types.TIME_WITH_TIMEZONE),它仍会抛出不支持的类型异常。我可以在Spring配置中为postgres数据源设置stringtype = unspecified,并通过将参数作为ISO格式的字符串传递来解决问题。
但是,我正在尝试使用EmbeddedPostgres(来自OpenTable Embedded PostgreSQL Component)为我的数据访问层编写功能测试,因为测试将在AWS中运行而无需访问RDS实例。不幸的是,解决方法不起作用,因为EmbeddedPostgres会因设置字符串类型而窒息。
EmbeddedPostgres.builder().setServerConfig("stringtype", "unspecified").start();
// throws FATAL: unrecognized configuration parameter "stringtype"
有没有人知道这两个障碍中的任何一个的解决方法?要么让Postgres明确接受时间戳,要么让EmbeddedPostgres获取stringtype参数(根据documentation应该有效)。
谢谢!
答案 0 :(得分:0)
好的,经过多次撞击它之后,我找到了两个问题的解决方法。我会在这里张贴它们,以防万一其他人遇到同样的问题。
对于第一个问题:
parameters.registerSqlType(EVENT_DATE, Types.TIMESTAMP_WITH_TIMEZONE);
//throws "PSQLException: Unsupported Types value: 2,013,"
我发现即使我设置了一个直接的Timestamp对象,Postgres也会正确设置该值,只要我使用纪元时间值创建它即可。所以这有效:
parameters.registerSqlType(EVENT_DATE, Types.TIMESTAMP);
java.sql.Timestamp sqlTS = new Timestamp(evt.getEventDate().toEpochMilli());
parameters.addValue(EVENT_DATE, sqlTS);
关于第二个问题 - 在EmbeddedPostgres上设置配置参数,我意识到我正在设置服务器配置参数,而我需要的是一个Connection参数。
这里的代码不起作用:
EmbeddedPostgres pg = EmbeddedPostgres.builder().setServerConfig("stringtype", "unspecified").start();
// throws FATAL: unrecognized configuration parameter "stringtype"
DataSource source = pg.getPostgresDatabase();
但这确实有效:
EmbeddedPostgres pg = EmbeddedPostgres.builder().start();
Properties props = new Properties();
props.setProperty("stringtype","unspecified");
Connection conn = DriverManager.getConnection(pg.getJdbcUrl("postgres", "postgres"), props);
DataSource source = new SingleConnectionDataSource(conn, false);
因此,如果您需要设置任何postgres特定的连接参数,您只需要滚动自己的DataSource。
希望能帮助别人!