Java Database Connections & Transactions

Category: Make It So | Number of pages: 170 | Published: May, 2019

Why does Java database access have to be so messy?

Opening up a connection to a database with plain JDBC seems to be rather straightforward.

But then you have all the intricacies of transaction handling:

  • What are transaction propagation and isolation levels? When would you use which?
  • Why does your production code deadlock the DB?
  • What are savepoints and how could you use them?
  • Which connection pool should you use? How can you avoid connection leaks?
  • What about distributed transactions?

Frameworks make it even more confusing!

You might use the Spring framework and have so many @Transactional annotations that you are unsure where a transaction starts and where it ends. Or what happens if you nest those @Transactionals. What are TransactionSynchronizations again? And what is a TransactionManager? How does a framework like jOOQ work together with Spring and its transactions?

Let's not even start with Hibernate (or any other JPA implementation). You might have been using the SessionFactory or EntityManager for years but do you really have solid knowledge when it comes to Sessions vs Connections vs Transactions?

But it doesn't have to be that way

When it comes to Java database access there's a ton of questions. Wouldn't it be nice to have straightforward practical answers to all these questions? And not only read about these answers, but have short lessons with executable code that help you understand all concepts by heart?

You have come to the right place

This book is not(!) a dry 700 pages reference-manual. Instead it will guide you through your learning journey, step-by-step with readymade exercises. Every chapter is an exercise with specific instructions of what to do, what to expect and study drills for yourself as homework. Sounds too good to be true? Check out the sample excerpt.

2nd version notes:

  • Updated all dependencies and affected code (Spring 5.1.x, Hibernate 5.4.x, Jooq 3.11.x, HikariCP 3.3.x).
  • Fixed a lot of typos.
  • Extended the DataSource chapter with an answer about validations/timeouts.
  • Better description of some study drills and better Q&As.
  • Better explanation of setFetchsize/Resultsets.
$25.00 Now $20.00
Buy Now Sample Chapter
  • Digital Edition for immediate download
  • PDF, Mobi, Epub & HTML versions included
  • 30-Day Money-Back Guarantee
  • Source Code
Share

Table of Contents

  • Acknowledgements
  • Preface
  • 2nd version updates
  • The Setup
    • Software Requirements
    • Code Requirements
  • Plain JDBC
    • Setup
    • Opening A Connection To The Database
    • Working Without Transactions
    • Starting And Committing A Transaction
    • Let Us Rollback
    • Multiple Users And Deadlocks - Inserts (1)
    • Multiple Users And Deadlocks - Inserts (2)
    • Multiple Users And Deadlocks - Inserts (3)
    • Multiple Users And Deadlocks - Updates
    • Multiple Users And Deadlocks - DDLs
    • Pessimistic Locking
    • Optimistic Locking
    • Isolation Levels
    • Savepoints
    • Summary
  • Additional Topics
    • Setup
    • JDBC Datasources
    • Using A JDBC Connection Pool
    • JDBC driver logging
    • Large JDBC ResultSets
    • Summary
  • Spring
    • Setup
    • Spring - Creating a DataSource and TransactionManager
    • @Transactional ( propagation = Propagation.REQUIRED )
    • Calling yourself & Proxies
    • @Transactional ( propagation = Propagation.REQUIRES_NEW )
    • @Transactional ( propagation = Propagation.NESTED )
    • Isolation Levels With Spring
    • Programmatic Transactions with Spring
    • TransactionSynchronizations - or, how to hook into Transactions
    • Transactional Tests with Spring
    • Spring JDBC Logging
    • Summary
  • Hibernate/JPA
    • Setting up Hibernate with Spring
    • Hibernate Session vs JDBC Connection
    • Hibernate Stateless Sessions
    • Hibernate Transactions vs JDBC Transactions
    • Hibernate Sessions and Spring @Transactional
    • LazyInitializationException and what to do about it
    • Summary
  • JooQ
    • Setting up JooQ
    • JooQ Transactions
    • Setting up JooQ and Spring
    • JooQ and Spring transaction management
    • Summary
  • Distributed Transactions
    • Distributed Transactions - Bitronix, PoolingDataSources and JTATransaction Managers
    • Opening transactions and committing across two DataSources
    • Summary
  • The Final Curtain