`

java ee transaction and datasource concepts

阅读更多
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?
Read-Commit: a SELECT query only sees data committed before the query began.
This prevents Dirty Read(reading uncommitted data), but there may be Non-Repeatable Read (reading the same record might return different data in same transaction) and Phantom Read (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 Required: 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.

Support: if a transaction available, the ejb join the transaction.
RequiresNew: 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.

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

JPA implementation: 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 owned by the entity 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().


 
分享到:
评论

相关推荐

    The Java EE 6 Tutorial Basic Concepts 4th Edition

    Java EE Application Assembly and Deployment 17 Packaging Applications 17 Development Roles 19 Java EE 6 APIs 22 Java EE 6 APIs in the Java Platform, Standard Edition 6.0 31 GlassFish Server Tools...

    Transaction Processing Concepts and Techniques

    事务处理:概念与技术 英文版pdf,本书列举了大量成功的商业和研究系统的实例,此外,列出了许多事务处理算法的可编译的C代码片段。本书对于那些对实现分布式系统或客户-服务器结构感兴趣的人来说,是值得一读的。...

    The Java EE 6 Tutorial: Basic Concepts, 4th Edition

    《Java EE 6 Tutorial: Basic Concepts, Fourth Edition》是一本面向新手及中级Java开发者的指南书籍,旨在帮助他们深入理解Java平台企业版6(Java EE 6)的各项特性与技术。本书由Oracle公司Java EE 6文档团队成员...

    Transaction Processing Concepts and Techniques 英文版

    《Transaction Processing Concepts and Techniques》是一本数据库领域的经典著作,作者以深入浅出的方式阐述了数据库事务处理的理论和实践。这本书对于理解分布式系统和客户-服务器架构中的事务管理至关重要。 在...

    Transaction Processing_ Concepts and Techniques

    JimGray大神事务神作,数据库从业人员必备书籍,本版本是英文原版djvu版本,大家可自己转换为PDF。

    transaction-processing-concepts-and-techniques

    transaction-processing-concepts-and-techniques

    Java EE technologies and migrating Java EE

    在"Java EE technologies and migrating Java EE"的主题中,我们关注的是如何利用这些技术以及如何将现有的Java EE应用迁移到新的架构或平台。Java EE的发展历程中,经历了多次重大更新,例如从Java EE 6到Java EE 8...

    精通Java EE:精通Java EE 整合应用案例\源代码第五章

    5. **JNDI(Java Naming and Directory Interface)**:JNDI在Java EE中用于查找和绑定资源,如数据源或EJB。开发者会学习如何使用JNDI查找服务并进行依赖注入。 6. **JTA(Java Transaction API)**:在多组件协作...

    java ee api

    Java EE(Java Platform, Enterprise Edition)是Oracle公司提供的一个企业级应用开发平台,它构建在Java SE(标准版)的基础上,为开发和部署分布式、多层的企业级应用程序提供了丰富的API和工具支持。Java EE旨在...

    Beginning JAVA EE 7经典

    涵盖了Java EE核心技术如JSF(JavaServer Faces)、CDI(Contexts and Dependency Injection)、EJB(Enterprise JavaBeans)、JPA(Java Persistence API)、JTA(Java Transaction API)的讲解,并且对于Java EE中...

    精通Java EE:精通Java EE 整合应用案例\源代码\源代码2-9章.

    Java EE,全称为Java Platform, Enterprise Edition,是Java平台针对企业级应用开发的版本,它提供了丰富的组件和服务,用于构建分布式、多层架构的企业应用程序。本资料包包含"精通Java EE:精通Java EE 整合应用...

    JAVA EE中文文档

    JAVA EE,全称为Java Platform, Enterprise Edition,是Java平台企业版的简称,是Oracle公司推出的一种用于构建企业级分布式应用程序的框架。它为开发者提供了在服务器端开发应用程序的一系列标准和API,包括Web组件...

    Java EE期末考试试题

    7. **JTA (Java Transaction API)**: 事务管理是Java EE应用中不可或缺的一部分。JTA用于管理分布式事务,确保数据的一致性和完整性。你需要知道ACID属性,并理解如何配置和使用JTA进行事务控制。 8. **JPA (Java ...

    java EE教程

    Java EE(Java Platform, Enterprise Edition)是Oracle公司提供的一个企业级应用开发平台,它构建在Java SE基础之上,为开发和部署分布式、多层的企业级应用程序提供了丰富的API和工具。本教程旨在帮助读者全面理解...

    java ee教程(电子教案)

    7. **CDI(Contexts and Dependency Injection)**:CDI为Java EE应用提供依赖注入和上下文管理,简化组件之间的依赖关系和生命周期管理。 8. **WS(Web Services)**:Java EE支持SOAP和RESTful两种类型的Web服务...

    java EE帮助文档

    Java EE(Java Platform, Enterprise Edition),也常被称为J2EE,是Java技术在企业级应用开发中的核心框架。它提供了一系列标准和API,用于构建分布式、多层的企业级应用程序,涵盖了从数据库连接、Web服务到安全...

    精心整理的Java EE API中文版帮助文档!!

    10. **CDI(Contexts and Dependency Injection)**:CDI是Java EE的依赖注入框架,它提供了管理对象生命周期和依赖关系的能力,使得应用更易于测试和维护。 11. **JAX-RS(Java API for RESTful Web Services)**...

    The Java EE 5Tutorial

    8. **Transaction Management**:Java EE 5提供了事务管理机制,确保在分布式环境中数据的一致性和完整性。开发者可以通过声明式或编程式的方式来控制事务。 9. **Security**:Java EE 5包含一套全面的安全模型,...

Global site tag (gtag.js) - Google Analytics