Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 EJB 层技术 (II) Transaction...
-
Upload
emil-shields -
Category
Documents
-
view
314 -
download
5
Transcript of Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 EJB 层技术 (II) Transaction...
Review: J2EE Framework
(X)HTMLXML
Applet
ClientApplication
JAF
JMS
JDB
C
JTA
JND
I
JSTL
Servlets JSPs
Web Container
SessionBeans
EntityBeans
EJB Container
J2EEApplicationServer
RDMS
Mail Server
JavaApplication
CORBAServer
DirectoryService
Message Queue
JDBC
JavaMail
RMI
IIOP
JNDI
JMS
HTTP
MessageBeans
Application Client Container
JAXRPC
SAAJ
JAX
RJA
CC
Mgmt
JMX
JAF
JMS
JDB
C
JTA
JND
I
JAXRPC
SAAJ
JAX
RJA
CC
Mgmt
JMX
JAXRPC
SAAJ
JAX
RJM
S
Mgmt
JMX
Web Application Architecture: MVC Model
Model• Encapsulates application state
•Responds to state queries• Exposes application functionality
•Notifies views of changes
Model• Encapsulates application state
•Responds to state queries• Exposes application functionality
•Notifies views of changes
View• Renders the models
• Request updates from models• Sends user gestures to Controller
• Allows controller to select View
View• Renders the models
• Request updates from models• Sends user gestures to Controller
• Allows controller to select View
Controller• Define application behavior
• Maps user actions to model updates• Select view for response•One for each functionality
Controller• Define application behavior
• Maps user actions to model updates• Select view for response•One for each functionality
State Query State Change
ViewSelection
User gestures
Change Notice
Transaction Conceptbegin transaction
debit checking account
credit savings account
update history log
commit transaction
CMT Transaction Managementpublic void transferToSaving(double amount) throws InsufficientBalanceException { checkingBalance -= amount; savingBalance += amount; try { updateChecking(checkingBalance); if (checkingBalance < 0.00) { context.setRollbackOnly(); throw new InsufficientBalanceException(); } updateSaving(savingBalance); } catch (SQLException ex) { throw new EJBException ("Transaction failed due to SQLException: " + ex.getMessage()); }}
BMT Transaction Management : JDBC
public void ship (String productId, String orderId, int quantity) { try { con.setAutoCommit(false); updateOrderItem(productId, orderId); updateInventory(productId, quantity); con.commit(); } catch (Exception ex) { try { con.rollback(); throw new EJBException("Transaction failed: " + ex.getMessage()); } catch (SQLException sqx) { throw new EJBException("Rollback failed: " + sqx.getMessage()); } }}
BMT Transaction Management : JTA
public void withdrawCash(double amount) {UserTransaction ut = context.getUserTransaction();try { ut.begin(); updateChecking(amount); machineBalance -= amount; insertMachine(machineBalance); ut.commit(); } catch (Exception ex) { try { ut.rollback(); } catch (SystemException syex) { throw new EJBException ("Rollback failed: " + syex.getMessage()); } throw new EJBException ("Transaction failed: " + ex.getMessage());}
Transaction Allowance
Bean Type Container-Managed
Bean-Managed
JTA
JDBC
Entity Y N N
Session Y Y Y
Message-driven
Y Y Y
Resource Connections
Resource Manager Type Connection Factory Type JNDI Subcontext
JDBC javax.sql.DataSource java:comp/env/jdbc
JMS
javax.jms.TopicConnectionFactory javax.jms.QueueConnectionFactory
java:comp/env/jms
JavaMail javax.mail.Session java:comp/env/mail
URL java.net.URL java:comp/env/url
Connector javax.resource.cci.ConnectionFactory java:comp/env/eis
JAXR Resource Adapter
javax.xml.registry.ConnectionFactory
java:comp/env/eis/JAXR
Database Connection
private String dbName = "java:comp/env/jdbc/SavingsAccountDB";
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
Connection con = ds.getConnection();
Programmatic Security in the EJB Tier
public String getUser() { return context.getCallerPrincipal().getName(); }
boolean result = context.isCallerInRole("Customer");
Packaging Modules
EJB 模块,其包含 ejb 文件及相应类 Web 模块,其包含网络层的组件及资源 应用客户模块,其包含应用客户类 资 源 适 配 器 模 块 , 其 包 含 java 连 接 器
(connector), 资源适配器,和帮助库函数及相关资源。
EJB and Web Services
J2EE1.4 Web Services
.Net Web Services
Perl Web Services
EJB2.1 Container
StatelessSession Bean
SOAP
SOAP
SOAP
Web Services Support Web services are Web-based enterprise applications
that use open, Extensible Markup Language (XML)-based standards and transport protocols to exchange data with calling clients.
The J2EE platform provides the XML APIs and tools you need to quickly design, develop, test, and deploy Web services and clients that fully interoperate with other Web services and clients running on Java-based or non-Java-based platforms.
It is easy to write Web services and clients with the J2EE XML APIs. No low-level programming is needed because the XML API implementations do the work of translating the application data to and from an XML-based data stream that is sent over the standardized XML-based transport protocols.
The translation of data to a standardized XML-based data stream is what makes Web services and clients written with the J2EE XML APIs fully interoperable.