Pengantar Transaksi di Jawa dan Musim Semi

1. Perkenalan

Dalam tutorial ini, kita akan memahami apa yang dimaksud dengan transaksi di Java. Dengan demikian kita akan memahami bagaimana melakukan transaksi lokal sumber daya dan transaksi global. Ini juga akan memungkinkan kami menjelajahi berbagai cara untuk mengelola transaksi di Java dan Spring.

2. Apa Itu Transaksi?

Transaksi di Jawa, seperti pada umumnya mengacu pada serangkaian tindakan yang semuanya harus berhasil diselesaikan. Oleh karena itu, jika satu atau lebih tindakan gagal, semua tindakan lainnya harus mundur sehingga status aplikasi tidak berubah . Ini diperlukan untuk memastikan bahwa integritas status aplikasi tidak pernah dikompromikan.

Selain itu, transaksi ini mungkin melibatkan satu atau lebih sumber daya seperti database, antrian pesan, sehingga menimbulkan berbagai cara untuk melakukan tindakan dalam transaksi. Ini termasuk melakukan transaksi lokal sumber daya dengan sumber daya individu. Alternatifnya, banyak sumber daya dapat berpartisipasi dalam transaksi global.

3. Sumber Daya Transaksi Lokal

Pertama-tama kita akan menjelajahi bagaimana kita dapat menggunakan transaksi di Java sambil bekerja dengan sumber daya individu. Di sini, kami mungkin memiliki beberapa tindakan individual yang kami lakukan dengan sumber daya seperti database . Namun, kita mungkin ingin hal itu terjadi sebagai satu kesatuan yang utuh, seperti dalam unit kerja yang tak terpisahkan. Dengan kata lain, kami ingin tindakan ini terjadi dalam satu transaksi.

Di Java, kami memiliki beberapa cara untuk mengakses dan mengoperasikan sumber daya seperti database. Makanya, cara kita menangani transaksi juga tidak sama. Di bagian ini, kita akan menemukan bagaimana kita dapat menggunakan transaksi dengan beberapa pustaka ini di Java yang cukup sering digunakan.

3.1. JDBC

Java Database Connectivity (JDBC) adalah API di Java yang menentukan cara mengakses database di Java . Vendor database yang berbeda menyediakan driver JDBC untuk menghubungkan ke database dengan cara vendor-agnostik. Jadi, kami mengambil Connection dari driver untuk melakukan operasi yang berbeda pada database:

JDBC memberi kita opsi untuk mengeksekusi pernyataan di bawah transaksi. The perilaku default dari Connection adalah auto-komit . Untuk memperjelas, artinya setiap pernyataan diperlakukan sebagai transaksi dan secara otomatis dilakukan setelah eksekusi.

Namun, jika kita ingin menggabungkan beberapa pernyataan dalam satu transaksi, ini juga dapat dicapai:

Connection connection = DriverManager.getConnection(CONNECTION_URL, USER, PASSWORD); try { connection.setAutoCommit(false); PreparedStatement firstStatement = connection .prepareStatement("firstQuery"); firstStatement.executeUpdate(); PreparedStatement secondStatement = connection .prepareStatement("secondQuery"); secondStatement.executeUpdate(); connection.commit(); } catch (Exception e) { connection.rollback(); }

Di sini, kami telah menonaktifkan mode koneksi otomatis . Karenanya, kita dapat secara manual mendefinisikan batas transaksi dan melakukan komit atau kembalikan . JDBC juga memungkinkan kita untuk menetapkan Savepoint yang memberi kita lebih banyak kontrol atas berapa banyak untuk rollback.

3.2. JPA

Java Persistence API (JPA) adalah spesifikasi di Java yang dapat digunakan untuk menjembatani kesenjangan antara model domain berorientasi objek dan sistem database relasional . Jadi, ada beberapa implementasi JPA yang tersedia dari pihak ketiga seperti Hibernate, EclipseLink, dan iBatis.

Di JPA, kita dapat mendefinisikan kelas reguler sebagai Entitas yang memberi mereka identitas persisten. Kelas EntityManager menyediakan antarmuka yang diperlukan untuk bekerja dengan banyak entitas dalam konteks persistensi . Konteks persistensi dapat dianggap sebagai cache tingkat pertama tempat entitas dikelola:

Konteks persistensi di sini dapat terdiri dari dua jenis, dengan cakupan transaksi atau cakupan yang diperluas. Konteks ketekunan cakupan transaksi terikat ke satu transaksi. Sementara konteks persistensi dengan cakupan yang diperluas dapat menjangkau beberapa transaksi. The lingkup standar dari konteks ketekunan adalah transaksi-lingkup .

Mari kita lihat bagaimana kita membuat EntityManager dan mendefinisikan batas transaksi secara manual:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa-example"); EntityManager entityManager = entityManagerFactory.createEntityManager(); try { entityManager.getTransaction().begin(); entityManager.persist(firstEntity); entityManager.persist(secondEntity); entityManager.getTransaction().commit(); } catch (Exceotion e) { entityManager.getTransaction().rollback(); }

Di sini, kami membuat EntityManager dari EntityManagerFactory dalam konteks konteks persistensi cakupan transaksi. Kemudian kita mendefinisikan batas transaksi dengan metode begin , commit, dan rollback .

3.3. JMS

Java Messaging Service (JMS) adalah spesifikasi di Java yang memungkinkan aplikasi untuk berkomunikasi secara asinkron menggunakan pesan . API memungkinkan kita untuk membuat, mengirim, menerima, dan membaca pesan dari antrian atau topik. Ada beberapa layanan pengiriman pesan yang sesuai dengan spesifikasi JMS termasuk OpenMQ, dan ActiveMQ.

JMS API mendukung penggabungan beberapa operasi pengiriman atau penerimaan dalam satu transaksi. Namun, pada dasarnya arsitektur integrasi berbasis pesan, produksi dan konsumsi pesan tidak dapat menjadi bagian dari transaksi yang sama . Cakupan transaksi tetap antara klien dan penyedia JMS:

JMS memungkinkan kita membuat Sesi dari Koneksi yang kita peroleh dari ConnectionFactory khusus vendor . Kami memiliki opsi untuk membuat Sesi yang ditransaksikan atau tidak . Untuk non-transaksi Sesi s , kami dapat lebih menentukan yang sesuai modus mengakui juga.

Mari kita lihat bagaimana kita dapat membuat Sesi yang ditransaksikan untuk mengirim banyak pesan dalam satu transaksi:

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(CONNECTION_URL); Connection connection = = connectionFactory.createConnection(); connection.start(); try { Session session = connection.createSession(true, 0); Destination = destination = session.createTopic("TEST.FOO"); MessageProducer producer = session.createProducer(destination); producer.send(firstMessage); producer.send(secondMessage); session.commit(); } catch (Exception e) { session.rollback(); }

Di sini, kami membuat MessageProducer untuk Tujuan dari jenis topik. Kami mendapatkan Tujuan dari Sesi yang kami buat sebelumnya. Kami selanjutnya menggunakan Sesi untuk menentukan batas transaksi menggunakan metode komit dan kembalikan .

4. Transaksi Global

Seperti yang kita lihat, transaksi lokal sumber daya memungkinkan kita untuk melakukan beberapa operasi dalam satu sumber daya sebagai satu kesatuan yang utuh. Namun, cukup sering kami menangani operasi yang mencakup berbagai sumber daya . Misalnya, operasi di dua database berbeda atau database dan antrian pesan. Di sini, dukungan transaksi lokal dalam sumber daya tidak akan cukup bagi kami.

Yang kami butuhkan dalam skenario ini adalah mekanisme global untuk membatasi transaksi yang mencakup banyak sumber daya yang berpartisipasi . Ini sering dikenal sebagai transaksi terdistribusi dan ada spesifikasi yang telah diusulkan untuk menanganinya secara efektif.

The XA Spesifikasi adalah salah satu spesifikasi seperti yang mendefinisikan seorang manajer transaksi untuk transaksi kontrol di beberapa sumber . Java memiliki dukungan yang cukup matang untuk transaksi terdistribusi yang sesuai dengan Spesifikasi XA melalui komponen JTA dan JTS.

4.1. JTA

Java Transaction API (JTA) is a Java Enterprise Edition API developed under the Java Community Process. It enables Java applications and application servers to perform distributed transactions across XA resources. JTA is modeled around XA architecture, leveraging two-phase commit.

JTA specifies standard Java interfaces between a transaction manager and the other parties in a distributed transaction:

Let's understand some of the key interfaces highlighted above:

  • TransactionManager: An interface which allows an application server to demarcate and control transactions
  • UserTransaction: This interface allows an application program to demarcate and control transactions explicitly
  • XAResource: The purpose of this interface is to allow a transaction manager to work with resource managers for XA-compliant resources

4.2. JTS

Java Transaction Service (JTS) is a specification for building the transaction manager that maps to the OMG OTS specification. JTS uses the standard CORBA ORB/TS interfaces and Internet Inter-ORB Protocol (IIOP) for transaction context propagation between JTS transaction managers.

At a high level, it supports the Java Transaction API (JTA). A JTS transaction manager provides transaction services to the parties involved in a distributed transaction:

Services that JTS provides to an application are largely transparent and hence we may not even notice them in the application architecture. JTS is architected around an application server which abstracts all transaction semantics from the application programs.

5. JTA Transaction Management

Now it's time to understand how we can manage a distributed transaction using JTA. Distributed transactions are not trivial solutions and hence have cost implications as well. Moreover, there are multiple options that we can choose from to include JTA in our application. Hence, our choice must be in the view of overall application architecture and aspirations.

5.1. JTA in Application Server

As we have seen earlier, JTA architecture relies on the application server to facilitate a number of transaction-related operations. One of the key services it relies on the server to provide is a naming service through JNDI. This is where XA resources like data sources are bound to and retrieved from.

Apart from this, we have a choice in terms of how we want to manage the transaction boundary in our application. This gives rise to two types of transactions within the Java application server:

  • Container-managed Transaction: As the name suggests, here the transaction boundary is set by the application server. This simplifies the development of Enterprise Java Beans (EJB) as it does not include statements related to transaction demarcation and relies solely on the container to do so. However, this does not provide enough flexibility for the application.
  • Bean-managed Transaction: Contrary to the container-managed transaction, in a bean-managed transaction EJBs contain the explicit statements to define the transaction demarcation. This provides precise control to the application in marking the boundaries of the transaction, albeit at the cost of more complexity.

One of the main drawbacks of performing transactions in the context of an application server is that the application becomes tightly coupled with the server. This has implications with respect to testability, manageability, and portability of the application. This is more profound in microservice architecture where the emphasis is more on developing server-neutral applications.

5.2. JTA Standalone

The problems we discussed in the last section have provided a huge momentum towards creating solutions for distributed transactions that does not rely on an application server. There are several options available to us in this regard, like using transaction support with Spring or use a transaction manager like Atomikos.

Let's see how we can use a transaction manager like Atomikos to facilitate a distributed transaction with a database and a message queue. One of the key aspects of a distributed transaction is enlisting and delisting the participating resources with the transaction monitor. Atomikos takes care of this for us. All we have to do is use Atomikos-provided abstractions:

AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean(); atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource"); DataSource dataSource = atomikosDataSourceBean;

Here, we are creating an instance of AtomikosDataSourceBean and registering the vendor-specific XADataSource. From here on, we can continue using this like any other DataSource and get the benefits of distributed transactions.

Similarly, we have an abstraction for message queue which takes care of registering the vendor-specific XA resource with the transaction monitor automatically:

AtomikosConnectionFactoryBean atomikosConnectionFactoryBean = new AtomikosConnectionFactoryBean(); atomikosConnectionFactoryBean.setXaConnectionFactory(new ActiveMQXAConnectionFactory()); ConnectionFactory connectionFactory = atomikosConnectionFactoryBean;

Here, we are creating an instance of AtomikosConnectionFactoryBean and registering the XAConnectionFactory from an XA-enabled JMS vendor. After this, we can continue to use this as a regular ConnectionFactory.

Now, Atomikos provides us the last piece of the puzzle to bring everything together, an instance of UserTransaction:

UserTransaction userTransaction = new UserTransactionImp();

Now, we are ready to create an application with distributed transaction spanning across our database and the message queue:

try { userTransaction.begin(); java.sql.Connection dbConnection = dataSource.getConnection(); PreparedStatement preparedStatement = dbConnection.prepareStatement(SQL_INSERT); preparedStatement.executeUpdate(); javax.jms.Connection mbConnection = connectionFactory.createConnection(); Session session = mbConnection.createSession(true, 0); Destination destination = session.createTopic("TEST.FOO"); MessageProducer producer = session.createProducer(destination); producer.send(MESSAGE); userTransaction.commit(); } catch (Exception e) { userTransaction.rollback(); }

Here, we are using the methods begin and commit in the class UserTransaction to demarcate the transaction boundary. This includes saving a record in the database as well as publishing a message to the message queue.

6. Transactions Support in Spring

We have seen that handling transactions are rather an involved task which includes a lot of boilerplate coding and configurations. Moreover, each resource has its own way of handling local transactions. In Java, JTA abstracts us from these variations but further brings provider-specific details and the complexity of the application server.

Spring platform provides us a much cleaner way of handling transactions, both resource local and global transactions in Java. This together with the other benefits of Spring creates a compelling case for using Spring to handle transactions. Moreover, it's quite easy to configure and switch a transaction manager with Spring, which can be server provided or standalone.

Spring provides us this seamless abstraction by creating a proxy for the methods with transactional code. The proxy manages the transaction state on behalf of the code with the help of TransactionManager:

The central interface here is PlatformTransactionManager which has a number of different implementations available. It provides abstractions over JDBC (DataSource), JMS, JPA, JTA, and many other resources.

6.1. Configurations

Let's see how we can configure Spring to use Atomikos as a transaction manager and provide transactional support for JPA and JMS. We'll begin by defining a PlatformTransactionManager of the type JTA:

@Bean public PlatformTransactionManager platformTransactionManager() throws Throwable { return new JtaTransactionManager( userTransaction(), transactionManager()); }

Here, we are providing instances of UserTransaction and TransactionManager to JTATransactionManager. These instances are provided by a transaction manager library like Atomikos:

@Bean public UserTransaction userTransaction() { return new UserTransactionImp(); } @Bean(initMethod = "init", destroyMethod = "close") public TransactionManager transactionManager() { return new UserTransactionManager(); }

The classes UserTransactionImp and UserTransactionManager are provided by Atomikos here.

Further, we need to define the JmsTemplete which the core class allowing synchronous JMS access in Spring:

@Bean public JmsTemplate jmsTemplate() throws Throwable { return new JmsTemplate(connectionFactory()); }

Here, ConnectionFactory is provided by Atomikos where it enables distributed transaction for Connection provided by it:

@Bean(initMethod = "init", destroyMethod = "close") public ConnectionFactory connectionFactory() { ActiveMQXAConnectionFactory activeMQXAConnectionFactory = new ActiveMQXAConnectionFactory(); activeMQXAConnectionFactory.setBrokerURL("tcp://localhost:61616"); AtomikosConnectionFactoryBean atomikosConnectionFactoryBean = new AtomikosConnectionFactoryBean(); atomikosConnectionFactoryBean.setUniqueResourceName("xamq"); atomikosConnectionFactoryBean.setLocalTransactionMode(false); atomikosConnectionFactoryBean.setXaConnectionFactory(activeMQXAConnectionFactory); return atomikosConnectionFactoryBean; }

So, as we can see, here we are wrapping a JMS provider-specific XAConnectionFactory with AtomikosConnectionFactoryBean.

Next, we need to define an AbstractEntityManagerFactoryBean that is responsible for creating JPA EntityManagerFactory bean in Spring:

@Bean public LocalContainerEntityManagerFactoryBean entityManager() throws SQLException { LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); entityManager.setDataSource(dataSource()); Properties properties = new Properties(); properties.setProperty( "javax.persistence.transactionType", "jta"); entityManager.setJpaProperties(properties); return entityManager; }

As before, the DataSource that we set in the LocalContainerEntityManagerFactoryBean here is provided by Atomikos with distributed transactions enabled:

@Bean(initMethod = "init", destroyMethod = "close") public DataSource dataSource() throws SQLException { MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource(); mysqlXaDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test"); AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean(); xaDataSource.setXaDataSource(mysqlXaDataSource); xaDataSource.setUniqueResourceName("xads"); return xaDataSource; }

Here again, we are wrapping the provider-specific XADataSource in AtomikosDataSourceBean.

6.2. Transaction Management

Having gone through all the configurations in the last section, we must feel quite overwhelmed! We may even question the benefits of using Spring after all. But do remember that all this configuration has enabled us abstraction from most of the provider-specific boilerplate and our actual application code does not need to be aware of that at all.

So, now we are ready to explore how to use transactions in Spring where we intend to update the database and publish messages. Spring provides us two ways to achieve this with their own benefits to choose from. Let's understand how we can make use of them:

  • Declarative Support

The easiest way to use transactions in Spring is with declarative support. Here, we have a convenience annotation available to be applied at the method or even at the class. This simply enables global transaction for our code:

@PersistenceContext EntityManager entityManager; @Autowired JmsTemplate jmsTemplate; @Transactional(propagation = Propagation.REQUIRED) public void process(ENTITY, MESSAGE) { entityManager.persist(ENTITY); jmsTemplate.convertAndSend(DESTINATION, MESSAGE); }

The simple code above is sufficient to allow a save-operation in the database and a publish-operation in message queue within a JTA transaction.

  • Programmatic Support

While the declarative support is quite elegant and simple, it does not offer us the benefit of controlling the transaction boundary more precisely. Hence, if we do have a certain need to achieve that, Spring offers programmatic support to demarcate transaction boundary:

@Autowired private PlatformTransactionManager transactionManager; public void process(ENTITY, MESSAGE) { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.executeWithoutResult(status -> { entityManager.persist(ENTITY); jmsTemplate.convertAndSend(DESTINATION, MESSAGE); }); }

So, as we can see, we have to create a TransactionTemplate with the available PlatformTransactionManager. Then we can use the TransactionTemplete to process a bunch of statements within a global transaction.

7. Afterthoughts

As we have seen that handling transactions, particularly those that span across multiple resources are complex. Moreover, transactions are inherently blocking which is detrimental to latency and throughput of an application. Further, testing and maintaining code with distributed transactions is not easy, especially if the transaction depends on the underlying application server. So, all in all, it's best to avoid transactions at all if we can!

But that is far from reality. In short, in real-world applications, we do often have a legitimate need for transactions. Although it's possible to rethink the application architecture without transactions, it may not always be possible. Hence, we must adopt certain best practices when working with transactions in Java to make our applications better:

  • One of the fundamental shifts we should adopt is to use standalone transaction managers instead of those provided by an application server. This alone can simplify our application greatly. Moreover, it's much suited for cloud-native microservice architecture.
  • Further, an abstraction layer like Spring can help us contain the direct impact of providers like JPA or JTA providers. So, this can enable us to switch between providers without much impact on our business logic. Moreover, it takes away the low-level responsibilities of managing the transaction state from us.
  • Lastly, we should be careful in picking the transaction boundary in our code. Since transactions are blocking, it's always better to keep the transaction boundary as restricted as possible. If necessary we should prefer programmatic over declarative control for transactions.

8. Conclusion

Singkatnya, dalam tutorial ini kita membahas transaksi dalam konteks Java. Kami melalui dukungan untuk transaksi lokal sumber daya individu di Jawa untuk sumber daya yang berbeda. Kami juga mencari cara untuk mencapai transaksi global di Jawa.

Selanjutnya, kami melalui berbagai cara untuk mengelola transaksi global di Jawa. Selain itu, kami memahami bagaimana Spring membuat transaksi di Java menjadi lebih mudah bagi kami.

Terakhir, kami mempelajari beberapa praktik terbaik saat menangani transaksi di Java.