Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

@Retention(CLASS) @Target({METHOD, TYPE})

public @interface Transactional

Runs this method – or every public method of this class – in a database transaction.

The build rewrites the compiled method: its body moves into a private method of its own, and the method keeps its name and wraps that body in a begin and a commit, with a rollback on failure. Everything is decided at build time – the propagation, the rules below, the exception types – and written in as constants and instanceof tests, so there is nothing to interpret on the call.

Because the method itself is rewritten rather than wrapped by a proxy, the transaction applies however it is called: from another bean, from this, from a private caller, or on an object the application built with new. That is the one place this differs from Spring, where a call through this silently skips the transaction.

Rollback follows Spring’s rule: an unchecked exception or an Error rolls back, a checked exception commits, and rollbackFor and noRollbackFor adjust that. When several listed types match the thrown one, the most specific wins.

Everything done through the server’s DataSource – its own methods, an entity manager’s daos, the transaction’s com.codename1.orm.session.Session – joins the transaction on the calling thread. A connection is borrowed only when the transaction first needs one.

Methods

public abstract Propagation propagation() default Propagation.REQUIREDWhat to do about a transaction that is already open.
public abstract boolean readOnly() default falseA hint that the method only reads.
public abstract int timeout() default -1Seconds the transaction may take before its commit is refused and it is rolled back.
public abstract Class[] rollbackFor() default {}Exception types that roll back, in addition to unchecked ones.
public abstract Class[] noRollbackFor() default {}Exception types that commit even though they would roll back.

Method details

propagation

public abstract Propagation propagation() default Propagation.REQUIRED
What to do about a transaction that is already open.

readOnly

public abstract boolean readOnly() default false
A hint that the method only reads. Engines that support a read-only transaction get one, which lets them skip work and refuse writes.

timeout

public abstract int timeout() default -1
Seconds the transaction may take before its commit is refused and it is rolled back. Zero or less means no limit.

rollbackFor

public abstract Class[] rollbackFor() default {}
Exception types that roll back, in addition to unchecked ones.

noRollbackFor

public abstract Class[] noRollbackFor() default {}
Exception types that commit even though they would roll back.