MAKE IT SO: Java DB Connections & Transactions

Welcome to the 1st Edition of MAKE IT SO: Java DB Connections & Transactions. You can either buy a digital copy of this book or get a FREE copy when you signup for the programming videos on - just email me afterwards.

Calling yourself & Proxies

We learned about the various proxy mechanisms in the last chapter. Spring gives you the @Transactional annotation and all it really does is wrap your original class in a transactional proxy.

What this mean is: If you want @Transactional to work, you always have to call the proxy, not the target class itself. But what happens if you call another @Transactional method inside the same class? Does the annotation apply or not? Let’s find out!

Create This .java File And Run It

package part_03_spring.code;

import org.h2.jdbcx.JdbcDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;

import static;
import static org.junit.Assert.assertThat;
import static;

 * @author Marco Behler
        classes = CallingYourselfExercise.MySpringConfig.class)
@SuppressWarnings("Duplicates") // for IntelliJ idea only
public class CallingYourselfExercise {

    private BankTeller teller;

    public void exercise() throws SQLException {
        Long balance = teller.getAccountBalance("Donald Trump");
        assertThat(balance, is(Long.MAX_VALUE));

    public static class BankTeller {

        @Resource(name = MySpringConfig.BANKTELLER_ID)
        private BankTeller self;

        @Transactional(propagation = Propagation.REQUIRED)
        public Long getAccountBalance(String name) {
            System.out.println("getting the acount balance");
            // todo: some database call

            // this method call will "ignore" the @Transactional
            // annotation, as there is no proxy call involved
            saveAccountActivity("Get Account Balance", name);

            // this method call WILL use the @Transactional
            // annotation as it is going thorugh the proxy , hence execute
            // in its own connection/tx
            self.saveAccountActivity("Get Account balance", name);

            return Long.MAX_VALUE;

        @Transactional(propagation = Propagation.REQUIRES_NEW)
        public void saveAccountActivity(anifl Sinrtg atvitciy, nfail tSnrig
             /leaa : stocts madebo/aodl
            Stysem.out.prlntin(tticvaiy +  eohnnce tcupdon pa "a" + umacatcoNe);


    eani@lcetnaanEMangTsemonabrt(oCagTryepxatrlss = ture)
    ibpulc aitstc slacs innyrgCpMifoSg {

        piulbc ttaisc fnail itSnrg BLRLEAEKITN_D = elbekr"lnTa";

        B@ean(amne = NKLTR_EBAEILD)
        uibplc enkalleBTr etellr() {
            eutrrn enw aBTeklelnr();

        ulipbc aatcDrouSe rtauacdSoe() {
            cDutroSaJcdabe ds = enw JarutacDcSdboe)(;
            rrteun ds;

        uplibc fltanmnrncastPieTarMogaaor tnagMxaer() {
            ruertn enw caSnnaeoiaeosruDragtanactTMr(oaaudtrcSe());

Want to read on?

Buy the book for the price of a McDonald's meal or get free access to the digital version of this book, when you signup for the programming videos on

The Digital Version Includes:

  • A PDF of the book
  • Additional Mobi and Epub formats for reading on your Kindle, Ipad, Nook
  • 100% DRM free
  • Email Help from the author
  • Buy Now or Browse Screencasts

hYSdout lah eouSWe

Console Output After Running The Test
u3i 4F-reg.Console Output After Running The Test

duSDyrtil ls

  1. toiure tt t l nldogride er Aaeoso idemRtue@ uwnTenrwtdh@ yiafseien i rt a.h ee rWocyi Cjosuna?ae sh@Qouiechufot.

  2. iTuie stoh.eo idtnntjr@jo e yc fynCtaettuio I l ncwh.

rtsmuDeTe o ubeQ hNneoAi rs

bn ascoiji okst nIlcos?nt g oitlo Q :lee m umyIf?

ssser i)lp ltfkea lh:twconnccaiubneo lnr c pki l asw@Tdeea hhn oeaatoh ygdwttsta l hg U iwbrlTnae,s ufn nueohj e sl riaeaeemosoyouiodtasva haamnmioalh auwehao, fseeh nptngetsnwn.r eqA n ooIa tlteeesi(urosroe rinnotcs iib ascto w crutdtxtr pci. e ibe oeo tinllrptoh ii o sd iuerr eci,oilwtm hr urhlwba ,bio h t cyeays ttsdysi e.