Quick revision
The short answer
We keep set-based, data-heavy logic in the database, where it runs far faster next to the data, and move branching business rules that change often into Java, where they are easier to test and change. Whatever stays is managed like code: versioned in the same migration tool as the schema, reviewed, tested with Testcontainers against the real engine including error paths, owned by a named team, and exposed through a clear contract for parameters, errors and transaction behaviour. Portability and the scarcity of database-skilled maintainers weigh on every procedure we decide to keep.
What to remember
In this answer, we decided which of 150 procedures to keep by keeping set-based, data-heavy logic near the data and moving frequently changing branching rules to Java, using shadow comparisons to switch safely. Procedures we keep are versioned in the schema migration tool, reviewed, tested with Testcontainers on the real engine including error paths, owned by a team, and described by a contract covering parameters, errors and transactions, while portability and skills limit how many we keep.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We keep set-based, data-heavy logic in the database, where it runs far faster next to the data, and move branching business rules that change often into Java, where they are easier to test and change. Whatever stays is managed like code: versioned in the same migration tool as the schema, reviewed, tested with Testcontainers against the real engine including error paths, owned by a named team, and exposed through a clear contract for parameters, errors and transaction behaviour. Portability and the scarcity of database-skilled maintainers weigh on every procedure we decide to keep.
Why this question is asked
Banks often have decades of business rules in stored procedures: interest calculation, fee rules, end-of-day posting. They work, they are fast, and few people dare to touch them. New teams arrive with Java skills and want the logic where they can test and deploy it. Both sides are partly right. Interviewers ask this design question to see whether we can replace an ideological argument with criteria and a management model. In this answer, we will look at what procedures are good and bad at, sort the 150 into keep and move, and then cover versioning, testing, ownership and contracts, and the longer-term cost of skills and portability.
1What Procedures Are Good and Bad At
A stored procedure runs inside the database process, next to the data. Its strength is set-based work: one statement that updates a million accounts, an aggregation over a day's transactions, or posting a batch where moving the rows to Java and back would cost network round trips and memory. For that kind of work, a procedure can be ten or a hundred times faster than the same logic in a service.
Its weaknesses show with branching rules. Logic such as which fee applies to this customer, depending on product, segment, country, promotions and date, is a tree of conditions that changes every quarter. In PL/SQL or PL/pgSQL it is harder to unit test, harder to review, harder to reuse with other Java code, and it ships on the database release cycle rather than with the service. These are exactly the rules new teams want to move, and they are usually right.
2Sorting the 150 Procedures
We go through the procedures with a few questions each. How much data does it touch per call? How often does its logic change? Does it contain branching business rules or mostly SQL? What calls it, and how critical and well tested is it? The answers put each procedure into one of three groups.
| Kind of procedure | Example | Decision |
|---|---|---|
| Set-based, data-heavy | End-of-day interest posting for 5M accounts | Keep in the database |
| Data integrity close to tables | Balance checks used by many writers | Keep, or use constraints |
| Branching rules that change often | Fee selection by product, segment, promo | Move to Java |
| Thin wrappers around one query | get_customer_by_id | Replace with JPA or SQL |
| Rarely used, poorly understood | Old migration helpers | Investigate, then retire |
3Moving Rules Safely
For a rule we move, the safest path runs both versions side by side. The Java implementation is written with unit tests derived from the procedure's behaviour, then deployed in shadow mode: production calls the procedure as before, also calls the Java code, and logs any difference. After a few weeks without differences, including month-end, the Java version becomes primary and the procedure is retired.
Notice that the shadow comparison only reads. It never writes twice, and the legacy result stays authoritative until the evidence says otherwise. In a bank, this kind of proof is also what auditors and risk teams want to see.
4Managing the Procedures We Keep
Procedures that stay must be treated as code. They live in the same migration tool as the schema, Flyway or Liquibase, in the service's repository, so a schema change and the procedure that depends on it ship together and go through the same code review. Flyway's repeatable migrations, files prefixed R__, are a good fit: they are re-applied whenever their content changes, so the file always holds the current definition.
Testing happens against the real engine. With Testcontainers, the build starts a PostgreSQL or Oracle container, applies all migrations, and runs tests that call each procedure through the same JPA code production uses, including error paths: insufficient funds, a missing account, a duplicate posting. An in-memory database cannot run PL/SQL, so there is no shortcut.
5Ownership and Contracts
Each kept procedure gets an owning team, listed next to it, so a question or a change request has somewhere to go. Its contract is written down like an API: parameters and their units, the errors it raises and what they mean, whether it commits or must run inside the caller's transaction, and which tables it reads and writes. Many past incidents with procedures come from undocumented transaction behaviour or side effects on tables nobody expected.
Java callers then go through one repository per procedure group, so the rest of the service never builds procedure calls by hand, and replacing a procedure later means changing one class.
- Change proposed by the owning teamor requested by a caller
- Edit the R__ migration filereviewed like any code
- Testcontainers tests on the real enginehappy path and error paths
- Deploy with the schema migrationsame pipeline as the service
- Contract updatedparameters, errors, transaction behaviour
5 steps in order. Play them, or step through them one at a time.
6Portability and Skills
Every procedure we keep ties the bank to one engine and to people who can maintain procedural database code. That is a real cost: PL/SQL expertise is becoming scarcer, and a future engine change would have to rewrite every one. So the bar for keeping a procedure is that it earns its place through performance or integrity, not just history. We also invest in the skill gap, by pairing Java developers with the database specialists on procedure changes, so knowledge does not sit with two people close to retirement.
7Follow-up questions
Are database constraints a better home for some rules?
Yes, for integrity rules that are always true, such as a balance never below an overdraft limit or unique account numbers. Check constraints, foreign keys and unique indexes are declarative, enforced for every writer, and need no procedure at all. They are often the best replacement for validation procedures.
How do we measure whether moving a procedure hurt performance?
Run the shadow comparison with timing as well as results, and load-test the Java path with production-like volumes before switching. For set-based work, compare the end-to-end batch time, because a Java loop over rows that replaced one statement is the classic regression.
What if the procedures have no tests today?
Then the first step is characterisation tests: tests that record what each procedure currently does for a range of inputs, right or wrong. They protect both the procedures we keep and the ones we move, because the Java version must match them until someone decides a behaviour was a bug.
8Conclusion
In this answer, we decided which of 150 procedures to keep by keeping set-based, data-heavy logic near the data and moving frequently changing branching rules to Java, using shadow comparisons to switch safely. Procedures we keep are versioned in the schema migration tool, reviewed, tested with Testcontainers on the real engine including error paths, owned by a team, and described by a contract covering parameters, errors and transactions, while portability and skills limit how many we keep.
Open with
I would not decide this as Java versus database; I would sort the procedures by what they do. Set-based, data-heavy work like end-of-day interest posting over millions of accounts stays in the database, because running it next to the data is much faster than pulling rows into a service.
Then, if they want more
Branching business rules that change often, like fee selection by product, segment and promotion, move to Java, where they are easier to unit test, review and release. Thin wrappers around one query get replaced by JPA, and integrity rules may become constraints.
For the moves I would run the Java version in shadow mode next to the procedure and compare results, including month-end, before switching. Procedures we keep are managed like code: in the same Flyway or Liquibase migrations as the schema, often as repeatable R__ files, reviewed, and tested with Testcontainers against the real engine, including error paths.
Each has an owning team and a written contract: parameters, errors, transaction behaviour and the tables it touches. And I would be explicit about the long-term cost: every procedure ties us to one engine and to scarce database skills, so it has to earn its place.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A bank's Java services call 150 stored procedures that hold core business rules. New...": https://scrivio-interview-library.netlify.app/databases/jpa-queries/cdc1adf3f0/