java ee transaction and datasource concepts

10 篇文章 0 订阅
1. What is a transaction?
A transaction groups units of work and save the them all at once, or at any failure, roll back all the changes.

2. What are the features of a transaction?
ACID: Atomicity, Consistency, Isolation and Durability.

3. What are the major issues in app development regarding transaction?
3.1 Atomicity: if necessary, group several updates into one transaction to make sure the changes are either all saved or rolled back.
3.2 Consistency: when multiple threads accessing same record, make sure no changes/updates are lost.

4. What is the usual isolation most RDBMS databases have?
[b]Read-Commit[/b]: a SELECT query only sees data committed before the query began.
This prevents [b]Dirty Read[/b](reading uncommitted data), but there may be [b]Non-Repeatable Read[/b] (reading the same record might return different data in same transaction) and [b]Phantom Read[/b] (ie, newly committed data becomes visible to the query that were not visible earlier in the transaction).

5. When a transaction starts?
When an application connects to a database and executes some query, a transaction starts.

6. What is the java API that represents a database transaction?
A jdbc connection represents a database transaction. Normally when you get a connection, the transaction starts; when you close the connection, the transaction ended.

7. What is the default commit mode for jdbc connection?
jdbc connection commit mode defaults to true. this means after a query completes, by default the transaction would try to commit.

8. What represents a transaction task in Java EE?
A transaction task is represented by a method that performs a database query. Typically, an EJB method is by default transaction aware and normally represents a transaction task.

9. What is local transaction?
Local transaction involves only one local database/resource manager.

10. What is global/distributed transaction?
Global/distributed transactions may involve more than one databases/resource managers. If global/distributed transactions involves more than one databases, it'll use Two-Phase commit.

11. What is jdbc transaction?
jdbc transaction by interface java.sql.Connection and can only work on one single database.

12. What is JTA (Java Transaction API)
JTA is java's transaction API for global/distributed transactions.

13. What is data source
Data source is used to manage database connections. Usually a pool of connections would be created for an application deployed to JEE containers. javax.sql.Datasource: A factory for connections to the physical data source that this DataSource object represents.

14. What is transaction management service provided by JEE containers?
Transaction management service is one of the most important services provided by most JEE compliant application servers. This service would monitor any objects/components that participate in a transaction and decides in the end if the transaction should commit or roll back. For a local JTA transaction, the transaction management service would make sure that the jdbc connection would be the same one for any objects that gets connection from configured data source.

15. What is implicit transaction management in app development?
Also called Container Managed Transactions(CMT). With the transaction management service provided by JEE containers, programming object don't need to manually start/commit a transaction manually. For instance, with the EJB technology, any ejb method implicitly start a transaction and the transaction would commit when the method completes.

16. What is explicit transaction management in app development?
Developers in their code manually start/commit a transaction. For EJBs, it must be of Bean Managed Transactions(BMT).

For jdbc connections:
connection.setAutoCommit(false);
....... // tasks to perform
connection.commit();

For JTA, it has an interface javax.transaction.UserTransaction:
// it can be injected with @Resource
@Resource UserTansaction utx;

// if in ejb, can get from ejb context
@Resource SessionContext ejbContext;
UserTransaction utx = ejbContext.getUserTransaction();

utx.begin();
...... // tasks to perform
utx.commit();

17. What is JEE transaction scope?
JEE transaction scope refers to the objects that participate in a transaction. For instance, session EJBs, entity beans etc.

18 What is JEE transaction scope propagation?
The same transaction (connection) would be passed(propagated) to all the participants of a transaction. This includes objects such as the EJBs that are called by the first EJB that starts the transaction and objects that have jdbc methods to access databases, and injected persistent context and etc. This provides Atomicity in that all the changes would be committed or rolled back.

19. What is default transaction attribute for EJBs?
default is [b]Required[/b]: when an EjB method is called, if there's already a transaction available, this transaction would be used; otherwise, a new transaction would be started. The same transaction would be passed to any other EJBs, objects that need access to the database.

[b]Support[/b]: if a transaction available, the ejb join the transaction.
[b]RequiresNew[/b]: the ejb would start a new transaction. if a transaction already exists, the transaction would be suspended and resume after the ejb's transaction completes.

20 What is Optimistic Locking?
Optimistic Locking is commonly used to ensure data Consistency when there are concurrent access to same record. It assumes no other threads would update the same record. Add a version column to the table in question, morally a numeric type, like long/integer etc. Before commit update, check this column has the same value as that of last read. If they match, the update would be successful and transaction committed. If they don't match, then the record has been modified by some other thread and an OptimisticException should be raised and depends on requirement, it can re-try or just notifies client program components or end users.

[u]Manual implementation:[/u] A typical update statement thus becomes(assume latest version=12345):
update student set name='john', version = version + 1
where id=120 and version=12345;

[u]JPA implementation[/u]: use annotation @Version on the column. This property does not means to be updated by your code. JPA would manage the column automatically for you. When a non-relationship persistent field or property is changed, OR when a relationship attribute [b]owned by the entity[/b] is modified, JPA will increment the version value.

21. What is JBoss no-tx-datasource?
When you configure a datasource like this in JBoss, you don't have JBoss' transaction management service available. You virtually said this: "I would like to manage transactions by myself". Why people want this? no idea.

22. What is JBoss local-tx-datasource?
This is the local JTA datasource configuration for JBoss and the transaction management service is turned on. This is the most commonly used datasource configuration if your application interacts with only one database.

22. What is JBoss xa-datasource?
If your application interacts with more than one databases, you need Two-Phase commit and this xa datasource configuration will do that.

23 System exception and Application exception
System exceptions represents unknown errors. System exceptions include java.lang.RuntimeException and its subclasses. EJBException is a subclass of RuntimeExcpetion and thus a system exception. When a System Exception is thrown, transaction would be rolled back.

Application exceptions are on the other hand part of your business logic and always delivered directly to client and would not cause a transaction to roll back, so that client code would have opportunity to catch the exception and recover from it based on business requirement. Application exceptions can be created by developers and normally extends java.lang.Exception.

24. What are the roll back API methods?
For Container Managed Transactions(CMT), it would catch any application exception interested and use EJBContext.setRollbackOnly() to roll back the transaction.

For Bean Managed Transactions(BMT), it would catch any application exception interested and use javax.transaction.UserTransaction.rollback().
交易处理(Transaction Processing)是指在计算机系统中管理和执行一系列事务的过程。事务是指一组相关的操作或任务,可以是从简单的读、写操作到复杂的业务流程。在现代的信息系统中,交易处理是非常重要的,涉及到数据的准确性、可靠性、一致性和并发控制等问题。 交易处理的核心概念包括ACID属性(原子性、一致性、隔离性和持久性)、并发控制、故障恢复和日志记录等。原子性保证了事务中的操作要么全部成功,要么全部失败,不存在中间状态。一致性保证了事务执行前后数据的一致性。隔离性确保了并发执行的事务彼此互不干扰。持久性保证了事务提交后,其结果会被永久保存。这些属性是确保交易处理正确执行的基础。 并发控制是交易处理中的一个重要技术,它解决了多个事务同时访问和修改共享数据时可能出现的问题。并发控制通过使用各种技术(如锁、乐观并发控制和多版本并发控制等)来保证事务的正确执行顺序和数据的正确性。 故障恢复是指在系统出现故障或异常情况时,能够将数据恢复到正常状态的过程。故障恢复通常使用日志记录技术,将事务的操作日志记录下来,以便在系统崩溃或异常时进行恢复操作。 日志记录是交易处理中的一项关键技术。它记录了事务的操作序列,包括读操作、写操作和事务的提交和撤销等信息。通过日志记录,可以保证交易的原子性、一致性和恢复性。 总而言之,交易处理是一个重要的概念和技术,在现代的信息系统中起着至关重要的作用。它涉及到事务的管理、数据的一致性和并发控制等方面,通过使用ACID属性、并发控制、故障恢复和日志记录等技术来确保交易的正确执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值