-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatasyncS
905 lines (609 loc) · 25.5 KB
/
DatasyncS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
Here’s how to rewrite the synchronization code using Spring Data and JdbcTemplate for cleaner and more maintainable code.
---
Spring Data/JdbcTemplate Implementation
1. Configuration: Define a Spring configuration for your data sources (Oracle and SQL Server).
2. Repository Layer: Use JdbcTemplate for interacting with the databases.
3. Service Layer: Implement the synchronization logic with transaction management.
4. Main Application: Orchestrate the process.
---
1. Spring Configuration
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
public DataSource oracleDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@//host:port/service");
dataSource.setUsername("oracle_user");
dataSource.setPassword("oracle_password");
return dataSource;
}
@Bean
public DataSource sqlServerDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl("jdbc:sqlserver://host:port;databaseName=yourDB");
dataSource.setUsername("sqlserver_user");
dataSource.setPassword("sqlserver_password");
return dataSource;
}
@Bean
public JdbcTemplate oracleJdbcTemplate(DataSource oracleDataSource) {
return new JdbcTemplate(oracleDataSource);
}
@Bean
public JdbcTemplate sqlServerJdbcTemplate(DataSource sqlServerDataSource) {
return new JdbcTemplate(sqlServerDataSource);
}
}
---
2. Repository Layer
Oracle Repository:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.sql.Timestamp;
import java.util.List;
@Repository
public class OracleRepository {
private final JdbcTemplate jdbcTemplate;
public OracleRepository(JdbcTemplate oracleJdbcTemplate) {
this.jdbcTemplate = oracleJdbcTemplate;
}
public List<Record> fetchSourceData(Timestamp lastUpdate) {
String query = "SELECT id, col1, col2, last_update FROM source_table WHERE last_update > ?";
return jdbcTemplate.query(query, new Object[]{lastUpdate}, (rs, rowNum) ->
new Record(
rs.getInt("id"),
rs.getString("col1"),
rs.getString("col2"),
rs.getTimestamp("last_update")
)
);
}
}
SQL Server Repository:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.sql.Timestamp;
@Repository
public class SqlServerRepository {
private final JdbcTemplate jdbcTemplate;
public SqlServerRepository(JdbcTemplate sqlServerJdbcTemplate) {
this.jdbcTemplate = sqlServerJdbcTemplate;
}
public Timestamp fetchMaxLastUpdate() {
String query = "SELECT MAX(last_update) FROM target_table";
return jdbcTemplate.queryForObject(query, Timestamp.class);
}
public void executeMergeQuery(String mergeQuery) {
jdbcTemplate.execute(mergeQuery);
}
}
---
3. Service Layer
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp;
import java.util.List;
@Service
public class DataSyncService {
private final OracleRepository oracleRepository;
private final SqlServerRepository sqlServerRepository;
public DataSyncService(OracleRepository oracleRepository, SqlServerRepository sqlServerRepository) {
this.oracleRepository = oracleRepository;
this.sqlServerRepository = sqlServerRepository;
}
@Transactional
public void syncData() {
// Fetch max last_update from SQL Server
Timestamp maxLastUpdate = sqlServerRepository.fetchMaxLastUpdate();
if (maxLastUpdate == null) {
maxLastUpdate = new Timestamp(0); // Default to epoch
}
// Fetch source data from Oracle
List<Record> sourceRecords = oracleRepository.fetchSourceData(maxLastUpdate);
// Process records in chunks
int batchSize = 500;
for (int i = 0; i < sourceRecords.size(); i += batchSize) {
List<Record> batch = sourceRecords.subList(i, Math.min(i + batchSize, sourceRecords.size()));
// Generate the dynamic CTE and execute the MERGE statement
String mergeQuery = generateMergeQuery(batch);
sqlServerRepository.executeMergeQuery(mergeQuery);
}
}
private String generateMergeQuery(List<Record> records) {
StringBuilder cteBuilder = new StringBuilder("WITH SourceCTE (id, col1, col2, last_update) AS (VALUES ");
for (int i = 0; i < records.size(); i++) {
Record record = records.get(i);
cteBuilder.append("(")
.append(record.getId()).append(", ")
.append("'").append(record.getCol1().replace("'", "''")).append("', ")
.append("'").append(record.getCol2().replace("'", "''")).append("', ")
.append("'").append(record.getLastUpdate()).append("')");
if (i < records.size() - 1) {
cteBuilder.append(", ");
}
}
cteBuilder.append(") ");
return cteBuilder.append("""
MERGE INTO target_table AS target
USING SourceCTE AS source
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET
target.col1 = source.col1,
target.col2 = source.col2,
target.last_update = source.last_update
WHEN NOT MATCHED THEN
INSERT (id, col1, col2, last_update)
VALUES (source.id, source.col1, source.col2, source.last_update);
""").toString();
}
}
---
4. Main Application
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DataSyncApplication implements CommandLineRunner {
private final DataSyncService dataSyncService;
public DataSyncApplication(DataSyncService dataSyncService) {
this.dataSyncService = dataSyncService;
}
public static void main(String[] args) {
SpringApplication.run(DataSyncApplication.class, args);
}
@Override
public void run(String... args) {
dataSyncService.syncData();
}
}
---
Benefits of Using Spring Data/JdbcTemplate
1. Simplified Configuration and Boilerplate: JDBC setup is handled declaratively.
2. Transaction Management: Spring ensures atomicity across chunks with @Transactional.
3. Dependency Injection: Makes it easier to test and maintain the code.
4. Clean Code: Separates concerns across configuration, repository, service, and main layers.
Would you like me to add more features, such as logging or error handling?
CREATE TABLE #SourceData (
id INT,
col1 NVARCHAR(255),
col2 NVARCHAR(255),
last_update DATETIME
);
-- Insert source data from Oracle into #SourceData
-- This can be done using JDBC or a bulk insert mechanism.
INSERT INTO #SourceData (id, col1, col2, last_update)
VALUES
(1, 'Value1', 'Value2', '2024-11-20 10:00:00'),
(2, 'Value3', 'Value4', '2024-11-20 11:00:00');
WITH SourceCTE AS (
SELECT id, col1, col2, last_update
FROM #SourceData
)
MERGE INTO target_table AS target
USING SourceCTE AS source
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET
target.col1 = source.col1,
target.col2 = source.col2,
target.last_update = source.last_update
WHEN NOT MATCHED THEN
INSERT (id, col1, col2, last_update)
VALUES (source.id, source.col1, source.col2, source.last_update);
Yes, transforming the source table data into a Common Table Expression (CTE) in SQL Server and then using it in a MERGE statement for an upsert operation is a powerful and efficient approach. You can achieve this by querying the source data and passing it to SQL Server via a staging mechanism or a direct connection (e.g., JDBC).
Here’s how you can implement this:
---
Steps to Transform and Merge Using a CTE:
1. Fetch Source Data: Query the source Oracle table for the records that need to be synchronized (last_update > max_last_update).
2. Prepare the CTE in SQL Server:
Use the fetched data from the source as the basis for the CTE.
This can be done by:
Inserting the data into a staging table in SQL Server, or
Directly embedding the data in the SQL Server query.
3. Merge the CTE with the Target Table:
Use a MERGE statement to insert new records and update existing ones.
---
SQL Server Implementation Example:
Using a Temporary Table or Staging Table
1. Fetch and load the data into a temporary table:
CREATE TABLE #SourceData (
id INT,
col1 NVARCHAR(255),
col2 NVARCHAR(255),
last_update DATETIME
);
-- Insert source data from Oracle into #SourceData
-- This can be done using JDBC or a bulk insert mechanism.
INSERT INTO #SourceData (id, col1, col2, last_update)
VALUES
(1, 'Value1', 'Value2', '2024-11-20 10:00:00'),
(2, 'Value3', 'Value4', '2024-11-20 11:00:00');
2. Define a CTE based on the temporary table:
WITH SourceCTE AS (
SELECT id, col1, col2, last_update
FROM #SourceData
)
3. Merge the CTE with the target table:
MERGE INTO target_table AS target
USING SourceCTE AS source
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET
target.col1 = source.col1,
target.col2 = source.col2,
target.last_update = source.last_update
WHEN NOT MATCHED THEN
INSERT (id, col1, col2, last_update)
VALUES (source.id, source.col1, source.col2, source.last_update);
4. Clean up:
DROP TABLE #SourceData;
---
Directly Embedding Source Data in a CTE
If the fetched source data is small and can be embedded directly in the SQL query:
WITH SourceCTE AS (
SELECT *
FROM (VALUES
(1, 'Value1', 'Value2', '2024-11-20 10:00:00'),
(2, 'Value3', 'Value4', '2024-11-20 11:00:00')
) AS SourceData (id, col1, col2, last_update)
)
MERGE INTO target_table AS target
USING SourceCTE AS source
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET
target.col1 = source.col1,
target.col2 = source.col2,
target.last_update = source.last_update
WHEN NOT MATCHED THEN
INSERT (id, col1, col2, last_update)
VALUES (source.id, source.col1, source.col2, source.last_update);
---
Advantages of Using CTE with MERGE:
1. Readability: The CTE structure makes your SQL query easier to understand and maintain.
2. Performance: CTEs allow you to define reusable datasets, and MERGE ensures efficient upsert operations.
3. Flexibility: Works well for both small datasets (direct embedding) and large datasets (via staging).
4. Transactional Consistency: The MERGE statement is atomic, reducing the risk of partial updates.
---
Key Considerations:
1. Error Handling in MERGE:
Ensure proper handling of potential conflicts, e.g., duplicate keys.
Wrap the MERGE statement in a transaction for rollback capability.
2. Staging Table Management:
Use temporary tables (#TempTable) for transient data or permanent staging tables for larger datasets.
3. Performance Optimization:
Index the target table and #SourceData (or equivalent) on join keys (id).
For large batches, consider batching the MERGE statement itself.
---
Would you like help implementing this in Java, such as generating the CTE dynamically based on fetched data?
To implement this in Java, you can dynamically construct and execute the SQL Server MERGE statement with a CTE based on data fetched from the Oracle source table. Here’s how you can do it:
---
Java Implementation
1. Fetch Data from Oracle
Fetch records from the Oracle source table using JDBC, based on the last_update column:
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
class Record {
int id;
String col1;
String col2;
Timestamp lastUpdate;
// Constructor, getters, and setters
public Record(int id, String col1, String col2, Timestamp lastUpdate) {
this.id = id;
this.col1 = col1;
this.col2 = col2;
this.lastUpdate = lastUpdate;
}
}
public List<Record> fetchSourceData(Connection oracleConnection, Timestamp lastUpdate) throws SQLException {
String query = "SELECT id, col1, col2, last_update FROM source_table WHERE last_update > ?";
List<Record> records = new ArrayList<>();
try (PreparedStatement pstmt = oracleConnection.prepareStatement(query)) {
pstmt.setTimestamp(1, lastUpdate);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
records.add(new Record(
rs.getInt("id"),
rs.getString("col1"),
rs.getString("col2"),
rs.getTimestamp("last_update")
));
}
}
}
return records;
}
---
2. Dynamically Construct the CTE and Merge Query
Using the fetched data, dynamically build a SQL MERGE statement with a CTE:
public String generateMergeQuery(List<Record> records) {
StringBuilder cteBuilder = new StringBuilder("WITH SourceCTE (id, col1, col2, last_update) AS (VALUES ");
StringBuilder mergeBuilder = new StringBuilder();
// Build CTE dynamically
for (int i = 0; i < records.size(); i++) {
Record record = records.get(i);
cteBuilder.append("(")
.append(record.id).append(", ")
.append("'").append(record.col1.replace("'", "''")).append("', ")
.append("'").append(record.col2.replace("'", "''")).append("', ")
.append("'").append(record.lastUpdate).append("')");
if (i < records.size() - 1) {
cteBuilder.append(", ");
}
}
cteBuilder.append(")");
// Build the MERGE query
mergeBuilder.append(cteBuilder)
.append(" MERGE INTO target_table AS target ")
.append(" USING SourceCTE AS source ")
.append(" ON target.id = source.id ")
.append(" WHEN MATCHED THEN ")
.append(" UPDATE SET ")
.append(" target.col1 = source.col1, ")
.append(" target.col2 = source.col2, ")
.append(" target.last_update = source.last_update ")
.append(" WHEN NOT MATCHED THEN ")
.append(" INSERT (id, col1, col2, last_update) ")
.append(" VALUES (source.id, source.col1, source.col2, source.last_update);");
return mergeBuilder.toString();
}
---
3. Execute the Query in SQL Server
Send the dynamically generated query to SQL Server via JDBC:
public void executeMergeQuery(Connection sqlServerConnection, String mergeQuery) throws SQLException {
try (PreparedStatement pstmt = sqlServerConnection.prepareStatement(mergeQuery)) {
pstmt.executeUpdate();
}
}
---
4. Putting It All Together
Here’s the complete workflow:
import java.sql.*;
import java.util.List;
public class DataSyncUtility {
public static void main(String[] args) {
try (Connection oracleConnection = DriverManager.getConnection("jdbc:oracle:thin:@//host:port/service", "user", "password");
Connection sqlServerConnection = DriverManager.getConnection("jdbc:sqlserver://host:port;databaseName=yourDB", "user", "password")) {
// Fetch the latest update timestamp from the target table
Timestamp maxLastUpdate = fetchMaxLastUpdate(sqlServerConnection);
// Fetch data from Oracle
List<Record> sourceRecords = fetchSourceData(oracleConnection, maxLastUpdate);
if (!sourceRecords.isEmpty()) {
// Generate the merge query
String mergeQuery = generateMergeQuery(sourceRecords);
// Execute the merge query in SQL Server
executeMergeQuery(sqlServerConnection, mergeQuery);
System.out.println("Data synchronization complete.");
} else {
System.out.println("No new data to synchronize.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static Timestamp fetchMaxLastUpdate(Connection sqlServerConnection) throws SQLException {
String query = "SELECT MAX(last_update) FROM target_table";
try (PreparedStatement pstmt = sqlServerConnection.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
return rs.getTimestamp(1);
}
}
return new Timestamp(0); // Default to epoch if no records exist
}
// Include the fetchSourceData, generateMergeQuery, and executeMergeQuery methods here
}
---
Considerations:
1. Sanitize Inputs: Ensure that values like col1 and col2 are properly sanitized to avoid SQL injection.
2. Batch Processing: For large datasets, divide records into batches and execute multiple merge operations.
3. Error Handling: Wrap the operations in a transaction to ensure consistency in case of errors.
4. Logging: Log the synchronization process for monitoring and debugging.
Let me know if you need additional clarification or enhancements!
Here’s how to rewrite the synchronization code using Spring Data and JdbcTemplate for cleaner and more maintainable code.
---
Spring Data/JdbcTemplate Implementation
1. Configuration: Define a Spring configuration for your data sources (Oracle and SQL Server).
2. Repository Layer: Use JdbcTemplate for interacting with the databases.
3. Service Layer: Implement the synchronization logic with transaction management.
4. Main Application: Orchestrate the process.
---
1. Spring Configuration
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
public DataSource oracleDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@//host:port/service");
dataSource.setUsername("oracle_user");
dataSource.setPassword("oracle_password");
return dataSource;
}
@Bean
public DataSource sqlServerDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl("jdbc:sqlserver://host:port;databaseName=yourDB");
dataSource.setUsername("sqlserver_user");
dataSource.setPassword("sqlserver_password");
return dataSource;
}
@Bean
public JdbcTemplate oracleJdbcTemplate(DataSource oracleDataSource) {
return new JdbcTemplate(oracleDataSource);
}
@Bean
public JdbcTemplate sqlServerJdbcTemplate(DataSource sqlServerDataSource) {
return new JdbcTemplate(sqlServerDataSource);
}
}
---
2. Repository Layer
Oracle Repository:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.sql.Timestamp;
import java.util.List;
@Repository
public class OracleRepository {
private final JdbcTemplate jdbcTemplate;
public OracleRepository(JdbcTemplate oracleJdbcTemplate) {
this.jdbcTemplate = oracleJdbcTemplate;
}
public List<Record> fetchSourceData(Timestamp lastUpdate) {
String query = "SELECT id, col1, col2, last_update FROM source_table WHERE last_update > ?";
return jdbcTemplate.query(query, new Object[]{lastUpdate}, (rs, rowNum) ->
new Record(
rs.getInt("id"),
rs.getString("col1"),
rs.getString("col2"),
rs.getTimestamp("last_update")
)
);
}
}
SQL Server Repository:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.sql.Timestamp;
@Repository
public class SqlServerRepository {
private final JdbcTemplate jdbcTemplate;
public SqlServerRepository(JdbcTemplate sqlServerJdbcTemplate) {
this.jdbcTemplate = sqlServerJdbcTemplate;
}
public Timestamp fetchMaxLastUpdate() {
String query = "SELECT MAX(last_update) FROM target_table";
return jdbcTemplate.queryForObject(query, Timestamp.class);
}
public void executeMergeQuery(String mergeQuery) {
jdbcTemplate.execute(mergeQuery);
}
}
---
3. Service Layer
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp;
import java.util.List;
@Service
public class DataSyncService {
private final OracleRepository oracleRepository;
private final SqlServerRepository sqlServerRepository;
public DataSyncService(OracleRepository oracleRepository, SqlServerRepository sqlServerRepository) {
this.oracleRepository = oracleRepository;
this.sqlServerRepository = sqlServerRepository;
}
@Transactional
public void syncData() {
// Fetch max last_update from SQL Server
Timestamp maxLastUpdate = sqlServerRepository.fetchMaxLastUpdate();
if (maxLastUpdate == null) {
maxLastUpdate = new Timestamp(0); // Default to epoch
}
// Fetch source data from Oracle
List<Record> sourceRecords = oracleRepository.fetchSourceData(maxLastUpdate);
// Process records in chunks
int batchSize = 500;
for (int i = 0; i < sourceRecords.size(); i += batchSize) {
List<Record> batch = sourceRecords.subList(i, Math.min(i + batchSize, sourceRecords.size()));
// Generate the dynamic CTE and execute the MERGE statement
String mergeQuery = generateMergeQuery(batch);
sqlServerRepository.executeMergeQuery(mergeQuery);
}
}
private String generateMergeQuery(List<Record> records) {
StringBuilder cteBuilder = new StringBuilder("WITH SourceCTE (id, col1, col2, last_update) AS (VALUES ");
for (int i = 0; i < records.size(); i++) {
Record record = records.get(i);
cteBuilder.append("(")
.append(record.getId()).append(", ")
.append("'").append(record.getCol1().replace("'", "''")).append("', ")
.append("'").append(record.getCol2().replace("'", "''")).append("', ")
.append("'").append(record.getLastUpdate()).append("')");
if (i < records.size() - 1) {
cteBuilder.append(", ");
}
}
cteBuilder.append(") ");
return cteBuilder.append("""
MERGE INTO target_table AS target
USING SourceCTE AS source
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET
target.col1 = source.col1,
target.col2 = source.col2,
target.last_update = source.last_update
WHEN NOT MATCHED THEN
INSERT (id, col1, col2, last_update)
VALUES (source.id, source.col1, source.col2, source.last_update);
""").toString();
}
}
---
4. Main Application
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DataSyncApplication implements CommandLineRunner {
private final DataSyncService dataSyncService;
public DataSyncApplication(DataSyncService dataSyncService) {
this.dataSyncService = dataSyncService;
}
public static void main(String[] args) {
SpringApplication.run(DataSyncApplication.class, args);
}
@Override
public void run(String... args) {
dataSyncService.syncData();
}
}
---
Benefits of Using Spring Data/JdbcTemplate
1. Simplified Configuration and Boilerplate: JDBC setup is handled declaratively.
2. Transaction Management: Spring ensures atomicity across chunks with @Transactional.
3. Dependency Injection: Makes it easier to test and maintain the code.
4. Clean Code: Separates concerns across configuration, repository, service, and main layers.
Would you like me to add more features, such as logging or error handling?
-- Step 1: Create the staging table (if not exists)
CREATE TABLE #staging_table (
id INT,
col1 NVARCHAR(255),
col2 NVARCHAR(255),
last_update DATETIME
);
-- Step 2: Load data into the staging table
INSERT INTO #staging_table (id, col1, col2, last_update)
VALUES (1, 'value1', 'value2', '2024-11-21 10:00:00');
-- Step 3: Perform the MERGE operation
MERGE INTO target_table AS target
USING #staging_table AS source
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET
target.col1 = source.col1,
target.col2 = source.col2,
target.last_update = source.last_update
WHEN NOT MATCHED THEN
INSERT (id, col1, col2, last_update)
VALUES (source.id, source.col1, source.col2, source.last_update);
-- Step 4: Truncate or drop the staging table
TRUNCATE TABLE #staging_table;