Apache camel getting Oracle database generated ID -
i using camel jdbc component insert record oracle table. insert uses sequence populate primary key id column.
insert my_table (id, data) values (my_seq.nextval, 'some data')
the relevant part of route looks below:
from("some end point here") .process(preinsertprocessor) .to("jdbc:mydatasource") .process(new processor() { public void process(exchange exchange) throws exception { logger.info("extracting database generated id"); // list null list<integer> ids = exchange.getin().getheader( jdbcconstants.jdbc_generated_keys_data, list.class); });
inside preinsertprocessir
set message body insert statement , set 2 header values instruct camel want generated id back:
message.setbody("insert my_table (id, data) values (my_seq.nextval, ?:data)"); message.setheader("data", "some data"); message.setheader(jdbc_retrieve_generated_keys, true); message.setheader(jdbc_generated_columns, new string[]{"id"});
end if @ logs can see:
[debug] org.apache.camel.component.bean.methodinfo - setting bean invocation result on out message: [message: insert my_table(id, data)values (my_seq.nextval, :?data] [debug] org.apache.camel.spring.spi.transactionerrorhandler - transaction begin (0x1de4bee0) redelivered(false) (messageid: id-melw1tygc2s-62650-1438583607644-0-8 on exchangeid: id-melw1tygc2s-62650-1438583607644-0-9)) [info ] au.com.nab.cls.router.non.repudiation.generatedidextractor - extracting database generated id [debug] org.apache.camel.processor.multicastprocessor - done sequential processing 1 exchanges [debug] org.apache.camel.spring.spi.transactionerrorhandler - transaction commit (0x1de4bee0) redelivered(false) (messageid: id:414d5120445041594855423120202020027844552045b302 on exchangeid: id-melw1tygc2s-62650-1438583607644-0-7))
if of logs insert executed , final processor should able generated id. in reality happens no record gets inserted , no id present in header of message. without final processor works fine.
obviously doing wrong here cannot figure out what. aware use message en-richer id before insert prefer avoid database trip.
thank in advance inputs.
update put break point in org.apache.camel.component.jdbc.jdbcproducer
, found out reason not having insert executed , consequently not getting id back.
// jdbcproducer code; creating prepared statement part if (shouldretrievegeneratedkeys) { ... } else if (expectedgeneratedcolumns instanceof string[]) { // execution gets herestatement ps = conn.preparestatement(preparedquery, (string[]) expectedgeneratedcolumns); ... } // expected count returned here 2 int expectedcount = ps.getparametermetadata().getparametercount(); if (expectedcount > 0) { ... // , here crash: // java.sql.sqlexception: number of parameters mismatch. expected: 2, was:1 getendpoint().getpreparestatementstrategy().populatestatement(ps, it, expectedcount); }
this research stopped digging in various 3 parties code not easy. suspect 1 of following 2 options cause:
- i still not doing right way
- a camel bug not work expected when header contains both named parameters , retrieve generated keys settings
please advise fix or work around.
thanks again
i ran this. workaround:
use camel-sql instead of camel-jdbc. add parameterscount
option endpoint url (in adddition setheader(sqlconstants.sql_retrieve_generated_keys, constant(true))
, setheader(sqlconstants.sql_generated_columns, constant(new string[] {"id_column_name"})
).
update: works 11.2.0.4
jdbc driver (does not work 12.2.0.1
jdbc driver).
Comments
Post a Comment