0
# Transaction Management
1
2
JTA API for declarative and programmatic transaction management with XA resource coordination.
3
4
## Core Interfaces
5
6
```java { .api }
7
public interface UserTransaction {
8
void begin() throws NotSupportedException, SystemException;
9
void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException;
10
void rollback() throws IllegalStateException, SecurityException, SystemException;
11
void setRollbackOnly() throws IllegalStateException, SystemException;
12
int getStatus() throws SystemException;
13
void setTransactionTimeout(int seconds) throws SystemException;
14
}
15
16
public interface TransactionManager extends UserTransaction {
17
Transaction getTransaction() throws SystemException;
18
void setTransactionTimeout(int seconds) throws SystemException;
19
Transaction suspend() throws SystemException;
20
void resume(Transaction tobj) throws InvalidTransactionException, IllegalStateException, SystemException;
21
}
22
23
public interface Transaction {
24
void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, SystemException;
25
void rollback() throws IllegalStateException, SystemException;
26
void setRollbackOnly() throws IllegalStateException, SystemException;
27
int getStatus() throws SystemException;
28
boolean enlistResource(XAResource xaRes) throws RollbackException, IllegalStateException, SystemException;
29
boolean delistResource(XAResource xaRes, int flag) throws IllegalStateException, SystemException;
30
void registerSynchronization(Synchronization sync) throws RollbackException, IllegalStateException, SystemException;
31
}
32
```
33
34
## Transaction Annotations
35
36
```java { .api }
37
@Target({ElementType.TYPE, ElementType.METHOD})
38
@Retention(RetentionPolicy.RUNTIME)
39
public @interface Transactional {
40
TxType value() default TxType.REQUIRED;
41
Class[] rollbackOn() default {};
42
Class[] dontRollbackOn() default {};
43
}
44
```
45
46
## Usage Example
47
48
```java
49
@Stateless
50
public class OrderService {
51
52
@Resource
53
private UserTransaction userTransaction;
54
55
@Transactional
56
public void processOrder(Order order) {
57
// Automatically wrapped in transaction
58
orderRepository.save(order);
59
inventoryService.updateStock(order.getItems());
60
}
61
62
public void manualTransaction() throws Exception {
63
userTransaction.begin();
64
try {
65
// Business logic
66
userTransaction.commit();
67
} catch (Exception e) {
68
userTransaction.rollback();
69
throw e;
70
}
71
}
72
}
73
```