Summary: Insert or update data in an external SQL database table based on business keys (conflict resolution).
URI Format
pfx-sql:upsert?table=myTable&businessKeys=sku&dataSource=myDb
Parameters
|
Parameter |
Type |
Default |
Required |
Description |
|---|---|---|---|---|
|
|
String |
|
No |
Name of the Spring DataSource bean |
|
|
String |
|
Yes |
Target database table |
|
|
String |
|
No |
Mapper ID for field transformation |
|
|
String |
|
Yes |
Comma-separated business key columns used for conflict resolution (determines whether to insert or update) |
|
|
SQLDialect |
|
No |
SQL dialect |
|
|
Integer |
|
No |
Batch size for database writes |
How It Works
-
For each row, the component checks whether a record with the same
businessKeysvalues already exists in the table. -
If a match is found, the existing record is updated.
-
If no match is found, a new record is inserted.
This is equivalent to INSERT ... ON DUPLICATE KEY UPDATE (MySQL) or MERGE (Oracle/MSSQL) or INSERT ... ON CONFLICT DO UPDATE (PostgreSQL).
Examples
Basic Upsert by SKU
<route id="exportToDb">
<from uri="timer://export?repeatCount=1"/>
<to uri="pfx-api:fetch?objectType=P&filter=allProducts"/>
<to uri="pfx-sql:upsert?table=products&mapper=dbMapper&businessKeys=sku&dataSource=myDb"/>
</route>
Upsert with Composite Business Key
<route id="upsertComposite">
<from uri="timer://export?repeatCount=1"/>
<to uri="pfx-api:fetch?objectType=CFLP"/>
<to uri="pfx-sql:upsert?table=price_list&businessKeys=sku,region&dataSource=myDb&dialect=POSTGRESQL"/>
</route>
Truncate Then Upsert Pattern
<route id="truncateAndUpsert">
<from uri="timer://fullSync?repeatCount=1"/>
<to uri="pfx-sql:truncate?table=products&dataSource=myDb"/>
<to uri="pfx-api:fetch?objectType=P"/>
<to uri="pfx-sql:upsert?table=products&businessKeys=sku&mapper=dbMapper&dataSource=myDb"/>
</route>
Common Pitfalls
|
Problem |
Cause |
Fix |
|---|---|---|
|
All rows inserted (no updates) |
|
Ensure |
|
Missing |
Required parameter not provided |
Add |
|
Performance issues on large datasets |
Too many rows in a single upsert |
Adjust |
|
Wrong columns updated |
Mapper not aligned with table schema |
Review and correct the mapper field mappings |
See Also
-
pfx-sql Component — Parent reference
-
pfx-sql:insert — Simple insert (no conflict resolution)