SlideShare a Scribd company logo
1 of 69
50 EJB 3 Best Practices in 50 
Minutes 
Michael Remijan – Fusion Technology Solutions 
Ryan Cuprak – Dassault Systemès
About Us 
• Michael 
• @mjremijan 
• mjremijan@yahoo.com 
• http://mjremijan.blogspot.com 
• http://linkedin.com/in/mjremijan 
• Ryan 
• @ctjava 
• rcuprak@gmail.com 
• http://www.cuprak.info
EJB 3 In Action 2nd Edition 
Updated For 
• EE 7 
• EJB 3.2
#1 Where to use EJBs 
• Full EE server not needed 
• Web profiles 
• @since 2009 
• Introduced in EE 6 
• JSF 2.2, CDI 1.1, EJB 3.2 Lite, JPA 2.1, JTA 1.2, bean 
validation. 
• TomEE 
• Resin 
• GlassFish 
• WebLogic 
• WildFly 
• JBoss App Server 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
@Singleton 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#2 Concurrency 
• Singleton beans are unique per deployment. 
• Default concurrency on singleton beans is WRITE. 
• Default type is CONTAINER 
• @ConcurrencyManagement 
• ConcurrencyManagementType.CONTAINER 
• Use @Lock(LockType.READ) – on non-mutable methods 
• Use @AccessTimeout() – to reduce the chances of a deadlock. 
• value = -1 – Wait indefinitely 
• value = 0 – Timeout immediately if locked 
• value = 1+ - Wait this many time units (default millis) 
• unit = java.util. concurrent.TimeUnit 
• ConcurrencyManagementType.BEAN 
• Program for multiple threads – use sparingly. 
• Don’t mix approaches! 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#3 Batch 
• Use Singletons to schedule batch jobs 
• Java Batch API (JSR 352) 
• @Startup – Container starts automatically 
• @Schedule – CRON scheduler 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#4 Batch Server 
• Common question 
• Does the batch API handle clustering? 
• No. 
• How do you handle deployment to a cluster? 
• Each cluster will run the job 
• Synchronization strategies 
• Database lock 
• @Resource environment entry 
• File system file/directory check 
• Avoid it all together 
• Dedicated server just for batch operations 
• Typically need special resources anyway 
• Separate from application server stack 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
@Stateful 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#5 JSF Bean 
• Stateful bean can store session state 
• Used directly by JSF 
• Target of JSF actions 
• Produce bean JSF pages want. 
• Use CDI instead 
• @SessionScoped 
• @ConversationScoped 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
@Stateless 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#6 Injection 
• Am I a POJO? 
• @Inject 
• Am I an EJB? 
• @EJB 
Answer is YES! 
• Both are valid 
• Prefer @Inject for 
• @Local, @LocalBean, no-interface EJB 
• Running in web profile 
• Must use @EJB for: 
• @Remote EJB 
• Running in full EE server 
• Remote XA Transactions 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#7 DAO Pattern 
• Do not implement DAOs as EJBs! 
• EJB chaining is bad for performance 
• Wait for pooled instance? 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne 
Pooled!
#8 Inject DAO 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#9 DTO Pattern 
• 15 years ago 
• EJBs are mysterious things 
• All EJB calls remote 
• Container managed Entity beans 
• SQL select for getter methods 
• Data Transfer Object (DTO) pattern created 
• Get massive object with one call 
• All data, even unrelated 
• Now 
• EJBs are POJOs 
• EJBs are local 
• EJBs are injectable 
• Avoid the DTO pattern 
• Multiple injects to get different data 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#10 @Observes 
• EJBs can observe CDI events 
• Method must be 
• Static 
|| method on @Local interface 
|| public method on @LocalBean 
|| public method on no-interace bean 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#11 @Named 
• Beware! 
• @Named on EJBs is convenient but dangerous. 
• Code below results in 2 transactions in rendering 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne 
2 Transactions
#12 Threading 
• Never been a good idea to do your own threading. 
• Even worse idea now 
• More communication between client and server 
• JAX-WS 
• JAX-RS 
• WebSockets 
• All fighting for threads 
• Threads must be managed 
• Use ManagedExecutorService 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#13 SOAP Web Service 
• Expose EJB directly as a Web Service 
• Basically RMI 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#14 REST Web Service 
• Do not expose EJB methods directly as RESTful services. 
• Aggregate operations into a single message. 
• Service should be a façade over EJBs. 
• RESTful services are stateless – no Stateful Session Beans. 
• Stay true to RESTful services design principles: 
• PUT/POST/GET/DELETE 
• Don’t let transport specific code creep into EJBs: 
• JsonGenerator, JsonGeneratorFactory, etc. 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#15 @Remote Beans 
• Full EE Server 
• EJBs are accessible remotely 
• Not available in web profile 
• Why? 
• Heavy CPU or data processing 
• Clients need to be transactional 
• @Remote 
• Must be on an interface. . .AccountService 
• Implementation Bean 
• Must implement interface. . .AccountServiceBean 
• No-interface bean not allowed 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#16 @Remote ejb-xml 
• ejb-jar.xml 
• Use <module-name> to control portable JNDI name 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#17 @Remote web.xml 
• web.xml 
• Make funny application-level lookups 
• Helps ensure you are getting the right bean 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#18 @Remote glassfish-web.xml 
• Application server specific mapping 
• Link funny name to real location in JNDI 
• Use EE standard global JNDI name 
• Avoid using proprietary names. . .they may change 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne 
<module-name>
#19 @Remote Inject as EJB 
• @Inject cannot be used 
• Proxied remote resource 
• Use @EJB with lookup 
• “module” is preferred over “comp” 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Security & 
Transactions 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#20 Security in beans 
• Define @DeclaredRoles 
• Protect with @RolesAllowed 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#21 Security mapping 
• Roles in code 
• Roles in your enterprise 
• Database, LDAP 
• Not necessarily the same 
• Develop code to meet security requirements 
• Map code roles to enterprise roles 
• Application server specific 
• glassfish-ejb-jar.xml 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#22 Transactions 
• This is the default 
• Let the container handle it 
• Avoid Bean Managed Transaction! 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#23 Transaction Rollback 
• Avoid programmatic rollback! 
• Error prone 
• Going to miss a rollback somewhere 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#24 Transactions and Exceptions 
• Don’t assume automatic rollback on Exceptions! 
• System Exceptions 
• Container managed 
• Rolled back 
• Bean managed 
• Marked for rollback 
• RemoteException, RuntimeException, EJBException, etc. 
• Application Exceptions 
• No automatic rollback of transaction 
• Use @ApplicationException 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#25 Isolation Levels 
• Configure your isolation levels on your JDBC pools 
• Glassfish 
• Use multiple Persistence Units and different pools to access 
different isolation levels. 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Transaction Isolation Level Description 
read-uncommitted Dirty reads, non-repeatable reads, and phantom 
reads can occur. 
read-committed Dirty reads are prevented; non-repeatable reads 
and phantom reads can occur. 
repeatable-read Dirty reads and non-repeatable reads are 
prevented; phantom reads can occur. 
serializable Dirty reads, non-repeatable reads and phantom 
reads are prevented. 
1. Dirty Reads: Technically speaking a dirty read happens when a transaction reads 
some data that is being changed by another transaction which is not committed yet. 
2. Non-Repeatable Reads: A Non-Repeatable Read happens when a transaction reads 
some records that another transaction is modifying. If the reader transaction tries the 
same query within the same transaction again, the result will be different compared 
to the first time. 
3. Phantom Reads: Occur when we are reading a set of records with a specific WHERE 
clause and another operation inserts new records matching our WHERE clause. Our 
transaction has read the records without including the record being inserted. 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#26 XADataSource 
• Mixing JMS and Database? 
• Make sure you configure an XADataSource 
• GlassFish 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#27 EJB Decoupling 
• Avoid unnecessary coupling 
• Use CDI Events 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#28 WebSockets 
• Exposing an EJB as a WebSocket endpoint? 
• Not well defined in the Java EE 7 specification 
• Implementation specific at this point 
• Invoking an EJB from a WebSocket endpoint 
• Use @Inject or @EJB 
• Inject into a WebSocket 
• Similar to JAX-RS 
• Stateful and SingleBeans are the best match. 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Messaging 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#29 Classic JMS 
• After 10 years, this classic code can go away! (Arun Gupta) 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#30 JMSContext 
• Greatly simplied API (Arun Gupta) 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#31 Poison JMS - Redelivery 
• Message that keeps getting re-delivered! 
• Use getJMSRedelivered() 
• Surround with try-catch 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne 
Wrong type!
#32 Poison JMS - Transaction 
• A JPA rollback will cause a JMS rollback 
• Use helper method with REQUIRES_NEW 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Interceptors 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#33 Class Interceptors 
• Simplified AOP interceptors for around-invoke 
• Invoked before any of the EJB’s methods are invoked 
• Not called again for internal method calls 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#34 Method Interceptors 
• Simplified AOP interceptors for around-invoke 
• Invoked before the specific EJB’s method is invoked 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#35 Default Interceptor 
• The ejb-jar.xml must be used for a default interceptor 
• Attaches to all methods of every bean 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#36 Interceptor Ordering 
• Called from most general to most specific 
• Default -> Class -> Method 
• Called in the order they appear in XML or annotations 
• Use ejb-jar.xml to change ordering 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#37 Interceptor Exclusion 
• All interceptors are applied 
• Exclusion by annotation 
• @ExcludeDefaultInterceptors 
• @ExcludeClassInterceptors 
• Exclusion by ejb-jar.xml 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Testing 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#38 Unit Testing 
• EJB classes are POJOs! 
• Unit test one class at a time 
• Mock external resources 
• JUnit, Mockito 
• maven-surefire-plugin 
• *Test.java 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#39 Integration Testing Embedded 
• Embedded container API 
• EJBContainer 
• Mimic (In-memory) resources 
• JUnit 
• maven-failsafe-plugin 
• *IT.java 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#40 Integration Testing Alternative 
• Alternatives are additional implementations of a Bean 
• Ignored by CDI when looking for an injection match 
• Enabled by beans.xml 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Packaging 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#41 EAR 
• Recall global JNDI lookup names for EJBs 
java:<namespace>/[app-name]/<module-name> 
/<bean-name>[!fully-qualified-interface-name] 
• Default value for [app-name] is EAR file name. 
• ejbrace-business-ear-1.0.0.0-SNAPSHOT.ear 
• Use application.xml 
• <application-name> 
• <display-name> 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#42 EJB-JAR 
• Recall global JNDI lookup names for EJBs 
java:<namespace>/[app-name]/<module-name> 
/<bean-name>[!fully-qualified-interface-name] 
• Default value for <module-name> is EJB-JAR file name. 
• ejbrace-ejb-1.0.0.0-SNAPSHOT.jar 
• Use ejb-jar.xml 
• <module-name> 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#43 JPA 
• Auto-scanning for entities not in the JAR with persistence.xml 
Core 
(Entites) 
Server 
(EJBs) 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne 
JavaFX 
(Client)
#44 Override Annotations 
• The ejb-jar.xml can override certain annotations 
• Method permission 
• Annotations allow all roles for testing 
• Production adds role security 
ebj-jar.xml glassfish-ejb-jar.xml 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#45 Alternatives Per Environment 
• The bean.xml can specify alternate beans 
• Have Maven builds for different environments 
• Alternatives provide environment specific implementations 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#46 Batching EAR 
• Don’t have batch jobs on you application stack 
• Avoid hacks to only get one instance to run 
• Dedicated batch server 
• Separate EJB-JAR project 
• Job XML documents 
• Readers 
• Processors 
• Writers 
• EAR project 
• Combines Batch EJB-JAR and application EJB-JAR 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Miscellaneous 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#47 Which Remote Technology? 
• Remote EJB, JAX-RS, JAX-WS, Servlets 
• Speed? 
75.00 
70.00 
65.00 
60.00 
55.00 
50.00 
45.00 
40.00 
35.00 
30.00 
Average call times (ms) 
0 2 4 6 8 10 12 
Remote EJB (Default Transaction) Remote Servlet Remote JAX-RS 
Remote JAX-WS Remote EJB (Transaction Never) Remote EJB (Transaction Supports) 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#48 JNDI Lookup 
• Injection is great 
• JNDI lookups still needed? 
• Changing environment properties 
• Non-container managed classes – new SomeBean() 
• Multiple resources determined by user input 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
#49 Securing RESTful Services 
• Use tokens with RESTful Web Services (OWASP). 
• Leverage JASPIC to setup JAAS environment.
#50 Timers 
• Use programmatic or declarative timers? 
• Programmatic - defined at runtime 
• Declarative – annotated methods on an EJB 
• Considerations: 
• Clustering 
• Persistence
#50 Timers
Final Thoughts 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Don’t Panic 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Buy the Book 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
Evangelize 
• EJB is a super technology 
Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne

More Related Content

Viewers also liked

Top 50 java ee 7 best practices [con5669]
Top 50 java ee 7 best practices [con5669]Top 50 java ee 7 best practices [con5669]
Top 50 java ee 7 best practices [con5669]Ryan Cuprak
 
Hybrid Mobile Development with Apache Cordova and Java EE 7 (JavaOne 2014)
Hybrid Mobile Development with Apache Cordova and Java EE 7 (JavaOne 2014)Hybrid Mobile Development with Apache Cordova and Java EE 7 (JavaOne 2014)
Hybrid Mobile Development with Apache Cordova and Java EE 7 (JavaOne 2014)Ryan Cuprak
 
Designing JEE Application Structure
Designing JEE Application StructureDesigning JEE Application Structure
Designing JEE Application Structureodedns
 
Crash Introduction to Modern Java Data Access: Understanding JPA, Hibernate, ...
Crash Introduction to Modern Java Data Access: Understanding JPA, Hibernate, ...Crash Introduction to Modern Java Data Access: Understanding JPA, Hibernate, ...
Crash Introduction to Modern Java Data Access: Understanding JPA, Hibernate, ...Vladimir Bacvanski, PhD
 
Hybrid Mobile Development with Apache Cordova and
Hybrid Mobile Development with Apache Cordova and Hybrid Mobile Development with Apache Cordova and
Hybrid Mobile Development with Apache Cordova and Ryan Cuprak
 
Jms deep dive [con4864]
Jms deep dive [con4864]Jms deep dive [con4864]
Jms deep dive [con4864]Ryan Cuprak
 
Spring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepSpring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepGuo Albert
 
Containerless in the Cloud with AWS Lambda
Containerless in the Cloud with AWS LambdaContainerless in the Cloud with AWS Lambda
Containerless in the Cloud with AWS LambdaRyan Cuprak
 
Faster java ee builds with gradle [con4921]
Faster java ee builds with gradle [con4921]Faster java ee builds with gradle [con4921]
Faster java ee builds with gradle [con4921]Ryan Cuprak
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShareSlideShare
 
CDI Best Practices with Real-Life Examples - TUT3287
CDI Best Practices with Real-Life Examples - TUT3287CDI Best Practices with Real-Life Examples - TUT3287
CDI Best Practices with Real-Life Examples - TUT3287Ahmad Gohar
 
Java EE Programming [EJB 3.0 and JPA] Using Eclipse and JBoss
Java EE Programming [EJB 3.0 and JPA] Using  Eclipse and JBossJava EE Programming [EJB 3.0 and JPA] Using  Eclipse and JBoss
Java EE Programming [EJB 3.0 and JPA] Using Eclipse and JBossIMC Institute
 
Backendless BaaS. Dinosaurus for Jeeconf 2013
Backendless BaaS. Dinosaurus for Jeeconf 2013Backendless BaaS. Dinosaurus for Jeeconf 2013
Backendless BaaS. Dinosaurus for Jeeconf 2013backendless
 
2015 JavaOne EJB/CDI Alignment
2015 JavaOne EJB/CDI Alignment2015 JavaOne EJB/CDI Alignment
2015 JavaOne EJB/CDI AlignmentDavid Blevins
 
Multi-Robot Task Allocation using Meta-heuristic Optimization
Multi-Robot Task Allocation using Meta-heuristic OptimizationMulti-Robot Task Allocation using Meta-heuristic Optimization
Multi-Robot Task Allocation using Meta-heuristic OptimizationMohamed Ghanem
 
Thailand ePayment infographic 2012 by Paysbuy.com
Thailand ePayment infographic 2012 by Paysbuy.comThailand ePayment infographic 2012 by Paysbuy.com
Thailand ePayment infographic 2012 by Paysbuy.comPAYSBUY Co.,Ltd.
 

Viewers also liked (20)

Top 50 java ee 7 best practices [con5669]
Top 50 java ee 7 best practices [con5669]Top 50 java ee 7 best practices [con5669]
Top 50 java ee 7 best practices [con5669]
 
Hybrid Mobile Development with Apache Cordova and Java EE 7 (JavaOne 2014)
Hybrid Mobile Development with Apache Cordova and Java EE 7 (JavaOne 2014)Hybrid Mobile Development with Apache Cordova and Java EE 7 (JavaOne 2014)
Hybrid Mobile Development with Apache Cordova and Java EE 7 (JavaOne 2014)
 
Designing JEE Application Structure
Designing JEE Application StructureDesigning JEE Application Structure
Designing JEE Application Structure
 
Java EE Pattern: The Boundary Layer
Java EE Pattern: The Boundary LayerJava EE Pattern: The Boundary Layer
Java EE Pattern: The Boundary Layer
 
Crash Introduction to Modern Java Data Access: Understanding JPA, Hibernate, ...
Crash Introduction to Modern Java Data Access: Understanding JPA, Hibernate, ...Crash Introduction to Modern Java Data Access: Understanding JPA, Hibernate, ...
Crash Introduction to Modern Java Data Access: Understanding JPA, Hibernate, ...
 
Hybrid Mobile Development with Apache Cordova and
Hybrid Mobile Development with Apache Cordova and Hybrid Mobile Development with Apache Cordova and
Hybrid Mobile Development with Apache Cordova and
 
Jms deep dive [con4864]
Jms deep dive [con4864]Jms deep dive [con4864]
Jms deep dive [con4864]
 
Spring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepSpring + JPA + DAO Step by Step
Spring + JPA + DAO Step by Step
 
Containerless in the Cloud with AWS Lambda
Containerless in the Cloud with AWS LambdaContainerless in the Cloud with AWS Lambda
Containerless in the Cloud with AWS Lambda
 
Faster java ee builds with gradle [con4921]
Faster java ee builds with gradle [con4921]Faster java ee builds with gradle [con4921]
Faster java ee builds with gradle [con4921]
 
EJB Programming Using Eclipse & JBoss
EJB Programming Using Eclipse & JBossEJB Programming Using Eclipse & JBoss
EJB Programming Using Eclipse & JBoss
 
Jeeconf 2015
Jeeconf 2015Jeeconf 2015
Jeeconf 2015
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 
CDI Best Practices with Real-Life Examples - TUT3287
CDI Best Practices with Real-Life Examples - TUT3287CDI Best Practices with Real-Life Examples - TUT3287
CDI Best Practices with Real-Life Examples - TUT3287
 
Gradle
GradleGradle
Gradle
 
Java EE Programming [EJB 3.0 and JPA] Using Eclipse and JBoss
Java EE Programming [EJB 3.0 and JPA] Using  Eclipse and JBossJava EE Programming [EJB 3.0 and JPA] Using  Eclipse and JBoss
Java EE Programming [EJB 3.0 and JPA] Using Eclipse and JBoss
 
Backendless BaaS. Dinosaurus for Jeeconf 2013
Backendless BaaS. Dinosaurus for Jeeconf 2013Backendless BaaS. Dinosaurus for Jeeconf 2013
Backendless BaaS. Dinosaurus for Jeeconf 2013
 
2015 JavaOne EJB/CDI Alignment
2015 JavaOne EJB/CDI Alignment2015 JavaOne EJB/CDI Alignment
2015 JavaOne EJB/CDI Alignment
 
Multi-Robot Task Allocation using Meta-heuristic Optimization
Multi-Robot Task Allocation using Meta-heuristic OptimizationMulti-Robot Task Allocation using Meta-heuristic Optimization
Multi-Robot Task Allocation using Meta-heuristic Optimization
 
Thailand ePayment infographic 2012 by Paysbuy.com
Thailand ePayment infographic 2012 by Paysbuy.comThailand ePayment infographic 2012 by Paysbuy.com
Thailand ePayment infographic 2012 by Paysbuy.com
 

Similar to 50 EJB Best Practices

Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write ModulesIcinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write ModulesIcinga
 
Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009pratiknaik
 
Spring - CDI Interop
Spring - CDI InteropSpring - CDI Interop
Spring - CDI InteropRay Ploski
 
Oracle Fuson Middleware Diagnostics, Performance and Troubleshoot
Oracle Fuson Middleware Diagnostics, Performance and TroubleshootOracle Fuson Middleware Diagnostics, Performance and Troubleshoot
Oracle Fuson Middleware Diagnostics, Performance and TroubleshootMichel Schildmeijer
 
Micronaut Deep Dive - Devoxx Belgium 2019
Micronaut Deep Dive - Devoxx Belgium 2019Micronaut Deep Dive - Devoxx Belgium 2019
Micronaut Deep Dive - Devoxx Belgium 2019graemerocher
 
日本一細かいJavaOne2011報告
日本一細かいJavaOne2011報告日本一細かいJavaOne2011報告
日本一細かいJavaOne2011報告心 谷本
 
Introduction to Micronaut - JBCNConf 2019
Introduction to Micronaut - JBCNConf 2019Introduction to Micronaut - JBCNConf 2019
Introduction to Micronaut - JBCNConf 2019graemerocher
 
STP 2014 - Lets Learn from the Top Performance Mistakes in 2013
STP 2014 - Lets Learn from the Top Performance Mistakes in 2013STP 2014 - Lets Learn from the Top Performance Mistakes in 2013
STP 2014 - Lets Learn from the Top Performance Mistakes in 2013Andreas Grabner
 
QA Fest 2017. Александр Хотемской. Современные возможности в организации Prot...
QA Fest 2017. Александр Хотемской. Современные возможности в организации Prot...QA Fest 2017. Александр Хотемской. Современные возможности в организации Prot...
QA Fest 2017. Александр Хотемской. Современные возможности в организации Prot...QAFest
 
Getting Started with Serverless Architectures
Getting Started with Serverless ArchitecturesGetting Started with Serverless Architectures
Getting Started with Serverless ArchitecturesAmazon Web Services
 
How to Build Single Page HTML5 Apps that Scale
How to Build Single Page HTML5 Apps that ScaleHow to Build Single Page HTML5 Apps that Scale
How to Build Single Page HTML5 Apps that ScalePhil Leggetter
 
Puppet Camp New York 2014: Streamlining Puppet Development Workflow
Puppet Camp New York 2014: Streamlining Puppet Development Workflow Puppet Camp New York 2014: Streamlining Puppet Development Workflow
Puppet Camp New York 2014: Streamlining Puppet Development Workflow Puppet
 
Steamlining your puppet development workflow
Steamlining your puppet development workflowSteamlining your puppet development workflow
Steamlining your puppet development workflowTomas Doran
 
Microservices: The Best Practices
Microservices: The Best PracticesMicroservices: The Best Practices
Microservices: The Best PracticesPavel Mička
 
Continuous Integration, the minimum viable product
Continuous Integration, the minimum viable productContinuous Integration, the minimum viable product
Continuous Integration, the minimum viable productJulian Simpson
 
Supporting large scale React applications
Supporting large scale React applicationsSupporting large scale React applications
Supporting large scale React applicationsMaurice De Beijer [MVP]
 
NA Developer Day - Taking your COBOL apps to Net & JVM
NA Developer Day - Taking your COBOL apps to Net & JVM NA Developer Day - Taking your COBOL apps to Net & JVM
NA Developer Day - Taking your COBOL apps to Net & JVM Micro Focus
 
Streams API (Web Engines Hackfest 2015)
Streams API (Web Engines Hackfest 2015)Streams API (Web Engines Hackfest 2015)
Streams API (Web Engines Hackfest 2015)Igalia
 
Case Study: Migrating Hyperic from EJB to Spring from JBoss to Apache Tomcat
Case Study: Migrating Hyperic from EJB to Spring from JBoss to Apache TomcatCase Study: Migrating Hyperic from EJB to Spring from JBoss to Apache Tomcat
Case Study: Migrating Hyperic from EJB to Spring from JBoss to Apache TomcatVMware Hyperic
 

Similar to 50 EJB Best Practices (20)

Test-Driven Sitecore
Test-Driven SitecoreTest-Driven Sitecore
Test-Driven Sitecore
 
Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write ModulesIcinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
 
Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009
 
Spring - CDI Interop
Spring - CDI InteropSpring - CDI Interop
Spring - CDI Interop
 
Oracle Fuson Middleware Diagnostics, Performance and Troubleshoot
Oracle Fuson Middleware Diagnostics, Performance and TroubleshootOracle Fuson Middleware Diagnostics, Performance and Troubleshoot
Oracle Fuson Middleware Diagnostics, Performance and Troubleshoot
 
Micronaut Deep Dive - Devoxx Belgium 2019
Micronaut Deep Dive - Devoxx Belgium 2019Micronaut Deep Dive - Devoxx Belgium 2019
Micronaut Deep Dive - Devoxx Belgium 2019
 
日本一細かいJavaOne2011報告
日本一細かいJavaOne2011報告日本一細かいJavaOne2011報告
日本一細かいJavaOne2011報告
 
Introduction to Micronaut - JBCNConf 2019
Introduction to Micronaut - JBCNConf 2019Introduction to Micronaut - JBCNConf 2019
Introduction to Micronaut - JBCNConf 2019
 
STP 2014 - Lets Learn from the Top Performance Mistakes in 2013
STP 2014 - Lets Learn from the Top Performance Mistakes in 2013STP 2014 - Lets Learn from the Top Performance Mistakes in 2013
STP 2014 - Lets Learn from the Top Performance Mistakes in 2013
 
QA Fest 2017. Александр Хотемской. Современные возможности в организации Prot...
QA Fest 2017. Александр Хотемской. Современные возможности в организации Prot...QA Fest 2017. Александр Хотемской. Современные возможности в организации Prot...
QA Fest 2017. Александр Хотемской. Современные возможности в организации Prot...
 
Getting Started with Serverless Architectures
Getting Started with Serverless ArchitecturesGetting Started with Serverless Architectures
Getting Started with Serverless Architectures
 
How to Build Single Page HTML5 Apps that Scale
How to Build Single Page HTML5 Apps that ScaleHow to Build Single Page HTML5 Apps that Scale
How to Build Single Page HTML5 Apps that Scale
 
Puppet Camp New York 2014: Streamlining Puppet Development Workflow
Puppet Camp New York 2014: Streamlining Puppet Development Workflow Puppet Camp New York 2014: Streamlining Puppet Development Workflow
Puppet Camp New York 2014: Streamlining Puppet Development Workflow
 
Steamlining your puppet development workflow
Steamlining your puppet development workflowSteamlining your puppet development workflow
Steamlining your puppet development workflow
 
Microservices: The Best Practices
Microservices: The Best PracticesMicroservices: The Best Practices
Microservices: The Best Practices
 
Continuous Integration, the minimum viable product
Continuous Integration, the minimum viable productContinuous Integration, the minimum viable product
Continuous Integration, the minimum viable product
 
Supporting large scale React applications
Supporting large scale React applicationsSupporting large scale React applications
Supporting large scale React applications
 
NA Developer Day - Taking your COBOL apps to Net & JVM
NA Developer Day - Taking your COBOL apps to Net & JVM NA Developer Day - Taking your COBOL apps to Net & JVM
NA Developer Day - Taking your COBOL apps to Net & JVM
 
Streams API (Web Engines Hackfest 2015)
Streams API (Web Engines Hackfest 2015)Streams API (Web Engines Hackfest 2015)
Streams API (Web Engines Hackfest 2015)
 
Case Study: Migrating Hyperic from EJB to Spring from JBoss to Apache Tomcat
Case Study: Migrating Hyperic from EJB to Spring from JBoss to Apache TomcatCase Study: Migrating Hyperic from EJB to Spring from JBoss to Apache Tomcat
Case Study: Migrating Hyperic from EJB to Spring from JBoss to Apache Tomcat
 

More from Ryan Cuprak

Jakarta EE Test Strategies (2022)
Jakarta EE Test Strategies (2022)Jakarta EE Test Strategies (2022)
Jakarta EE Test Strategies (2022)Ryan Cuprak
 
DIY Home Weather Station (Devoxx Poland 2023)
DIY Home Weather Station (Devoxx Poland 2023)DIY Home Weather Station (Devoxx Poland 2023)
DIY Home Weather Station (Devoxx Poland 2023)Ryan Cuprak
 
Why jakarta ee matters (ConFoo 2021)
Why jakarta ee matters (ConFoo 2021)Why jakarta ee matters (ConFoo 2021)
Why jakarta ee matters (ConFoo 2021)Ryan Cuprak
 
Polygot Java EE on the GraalVM
Polygot Java EE on the GraalVMPolygot Java EE on the GraalVM
Polygot Java EE on the GraalVMRyan Cuprak
 
Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Ryan Cuprak
 
Node.js Development with Apache NetBeans
Node.js Development with Apache NetBeansNode.js Development with Apache NetBeans
Node.js Development with Apache NetBeansRyan Cuprak
 
Preparing for java 9 modules upload
Preparing for java 9 modules uploadPreparing for java 9 modules upload
Preparing for java 9 modules uploadRyan Cuprak
 
Faster Java EE Builds with Gradle
Faster Java EE Builds with GradleFaster Java EE Builds with Gradle
Faster Java EE Builds with GradleRyan Cuprak
 
Faster Java EE Builds with Gradle
Faster Java EE Builds with GradleFaster Java EE Builds with Gradle
Faster Java EE Builds with GradleRyan Cuprak
 
Java EE 8 Update
Java EE 8 UpdateJava EE 8 Update
Java EE 8 UpdateRyan Cuprak
 
Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Ryan Cuprak
 
Java script nirvana in netbeans [con5679]
Java script nirvana in netbeans [con5679]Java script nirvana in netbeans [con5679]
Java script nirvana in netbeans [con5679]Ryan Cuprak
 
Developing in the Cloud
Developing in the CloudDeveloping in the Cloud
Developing in the CloudRyan Cuprak
 
Combining R With Java For Data Analysis (Devoxx UK 2015 Session)
Combining R With Java For Data Analysis (Devoxx UK 2015 Session)Combining R With Java For Data Analysis (Devoxx UK 2015 Session)
Combining R With Java For Data Analysis (Devoxx UK 2015 Session)Ryan Cuprak
 
JavaFX Versus HTML5 - JavaOne 2014
JavaFX Versus HTML5 - JavaOne 2014JavaFX Versus HTML5 - JavaOne 2014
JavaFX Versus HTML5 - JavaOne 2014Ryan Cuprak
 
JavaOne 2013: Organizing Your Local Community
JavaOne 2013: Organizing Your Local CommunityJavaOne 2013: Organizing Your Local Community
JavaOne 2013: Organizing Your Local CommunityRyan Cuprak
 

More from Ryan Cuprak (17)

Jakarta EE Test Strategies (2022)
Jakarta EE Test Strategies (2022)Jakarta EE Test Strategies (2022)
Jakarta EE Test Strategies (2022)
 
DIY Home Weather Station (Devoxx Poland 2023)
DIY Home Weather Station (Devoxx Poland 2023)DIY Home Weather Station (Devoxx Poland 2023)
DIY Home Weather Station (Devoxx Poland 2023)
 
Why jakarta ee matters (ConFoo 2021)
Why jakarta ee matters (ConFoo 2021)Why jakarta ee matters (ConFoo 2021)
Why jakarta ee matters (ConFoo 2021)
 
Polygot Java EE on the GraalVM
Polygot Java EE on the GraalVMPolygot Java EE on the GraalVM
Polygot Java EE on the GraalVM
 
Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)
 
Node.js Development with Apache NetBeans
Node.js Development with Apache NetBeansNode.js Development with Apache NetBeans
Node.js Development with Apache NetBeans
 
Preparing for java 9 modules upload
Preparing for java 9 modules uploadPreparing for java 9 modules upload
Preparing for java 9 modules upload
 
Faster Java EE Builds with Gradle
Faster Java EE Builds with GradleFaster Java EE Builds with Gradle
Faster Java EE Builds with Gradle
 
Java EE 8
Java EE 8Java EE 8
Java EE 8
 
Faster Java EE Builds with Gradle
Faster Java EE Builds with GradleFaster Java EE Builds with Gradle
Faster Java EE Builds with Gradle
 
Java EE 8 Update
Java EE 8 UpdateJava EE 8 Update
Java EE 8 Update
 
Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)
 
Java script nirvana in netbeans [con5679]
Java script nirvana in netbeans [con5679]Java script nirvana in netbeans [con5679]
Java script nirvana in netbeans [con5679]
 
Developing in the Cloud
Developing in the CloudDeveloping in the Cloud
Developing in the Cloud
 
Combining R With Java For Data Analysis (Devoxx UK 2015 Session)
Combining R With Java For Data Analysis (Devoxx UK 2015 Session)Combining R With Java For Data Analysis (Devoxx UK 2015 Session)
Combining R With Java For Data Analysis (Devoxx UK 2015 Session)
 
JavaFX Versus HTML5 - JavaOne 2014
JavaFX Versus HTML5 - JavaOne 2014JavaFX Versus HTML5 - JavaOne 2014
JavaFX Versus HTML5 - JavaOne 2014
 
JavaOne 2013: Organizing Your Local Community
JavaOne 2013: Organizing Your Local CommunityJavaOne 2013: Organizing Your Local Community
JavaOne 2013: Organizing Your Local Community
 

50 EJB Best Practices

  • 1. 50 EJB 3 Best Practices in 50 Minutes Michael Remijan – Fusion Technology Solutions Ryan Cuprak – Dassault Systemès
  • 2. About Us • Michael • @mjremijan • mjremijan@yahoo.com • http://mjremijan.blogspot.com • http://linkedin.com/in/mjremijan • Ryan • @ctjava • rcuprak@gmail.com • http://www.cuprak.info
  • 3. EJB 3 In Action 2nd Edition Updated For • EE 7 • EJB 3.2
  • 4. #1 Where to use EJBs • Full EE server not needed • Web profiles • @since 2009 • Introduced in EE 6 • JSF 2.2, CDI 1.1, EJB 3.2 Lite, JPA 2.1, JTA 1.2, bean validation. • TomEE • Resin • GlassFish • WebLogic • WildFly • JBoss App Server Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 5. @Singleton Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 6. #2 Concurrency • Singleton beans are unique per deployment. • Default concurrency on singleton beans is WRITE. • Default type is CONTAINER • @ConcurrencyManagement • ConcurrencyManagementType.CONTAINER • Use @Lock(LockType.READ) – on non-mutable methods • Use @AccessTimeout() – to reduce the chances of a deadlock. • value = -1 – Wait indefinitely • value = 0 – Timeout immediately if locked • value = 1+ - Wait this many time units (default millis) • unit = java.util. concurrent.TimeUnit • ConcurrencyManagementType.BEAN • Program for multiple threads – use sparingly. • Don’t mix approaches! Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 7. #3 Batch • Use Singletons to schedule batch jobs • Java Batch API (JSR 352) • @Startup – Container starts automatically • @Schedule – CRON scheduler Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 8. #4 Batch Server • Common question • Does the batch API handle clustering? • No. • How do you handle deployment to a cluster? • Each cluster will run the job • Synchronization strategies • Database lock • @Resource environment entry • File system file/directory check • Avoid it all together • Dedicated server just for batch operations • Typically need special resources anyway • Separate from application server stack Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 9. @Stateful Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 10. #5 JSF Bean • Stateful bean can store session state • Used directly by JSF • Target of JSF actions • Produce bean JSF pages want. • Use CDI instead • @SessionScoped • @ConversationScoped Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 11. @Stateless Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 12. #6 Injection • Am I a POJO? • @Inject • Am I an EJB? • @EJB Answer is YES! • Both are valid • Prefer @Inject for • @Local, @LocalBean, no-interface EJB • Running in web profile • Must use @EJB for: • @Remote EJB • Running in full EE server • Remote XA Transactions Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 13. #7 DAO Pattern • Do not implement DAOs as EJBs! • EJB chaining is bad for performance • Wait for pooled instance? Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne Pooled!
  • 14. #8 Inject DAO Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 15. #9 DTO Pattern • 15 years ago • EJBs are mysterious things • All EJB calls remote • Container managed Entity beans • SQL select for getter methods • Data Transfer Object (DTO) pattern created • Get massive object with one call • All data, even unrelated • Now • EJBs are POJOs • EJBs are local • EJBs are injectable • Avoid the DTO pattern • Multiple injects to get different data Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 16. #10 @Observes • EJBs can observe CDI events • Method must be • Static || method on @Local interface || public method on @LocalBean || public method on no-interace bean Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 17. #11 @Named • Beware! • @Named on EJBs is convenient but dangerous. • Code below results in 2 transactions in rendering Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne 2 Transactions
  • 18. #12 Threading • Never been a good idea to do your own threading. • Even worse idea now • More communication between client and server • JAX-WS • JAX-RS • WebSockets • All fighting for threads • Threads must be managed • Use ManagedExecutorService Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 19. Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 20. #13 SOAP Web Service • Expose EJB directly as a Web Service • Basically RMI Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 21. #14 REST Web Service • Do not expose EJB methods directly as RESTful services. • Aggregate operations into a single message. • Service should be a façade over EJBs. • RESTful services are stateless – no Stateful Session Beans. • Stay true to RESTful services design principles: • PUT/POST/GET/DELETE • Don’t let transport specific code creep into EJBs: • JsonGenerator, JsonGeneratorFactory, etc. Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 22. #15 @Remote Beans • Full EE Server • EJBs are accessible remotely • Not available in web profile • Why? • Heavy CPU or data processing • Clients need to be transactional • @Remote • Must be on an interface. . .AccountService • Implementation Bean • Must implement interface. . .AccountServiceBean • No-interface bean not allowed Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 23. #16 @Remote ejb-xml • ejb-jar.xml • Use <module-name> to control portable JNDI name Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 24. #17 @Remote web.xml • web.xml • Make funny application-level lookups • Helps ensure you are getting the right bean Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 25. #18 @Remote glassfish-web.xml • Application server specific mapping • Link funny name to real location in JNDI • Use EE standard global JNDI name • Avoid using proprietary names. . .they may change Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne <module-name>
  • 26. #19 @Remote Inject as EJB • @Inject cannot be used • Proxied remote resource • Use @EJB with lookup • “module” is preferred over “comp” Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 27. Security & Transactions Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 28. #20 Security in beans • Define @DeclaredRoles • Protect with @RolesAllowed Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 29. #21 Security mapping • Roles in code • Roles in your enterprise • Database, LDAP • Not necessarily the same • Develop code to meet security requirements • Map code roles to enterprise roles • Application server specific • glassfish-ejb-jar.xml Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 30. #22 Transactions • This is the default • Let the container handle it • Avoid Bean Managed Transaction! Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 31. #23 Transaction Rollback • Avoid programmatic rollback! • Error prone • Going to miss a rollback somewhere Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 32. #24 Transactions and Exceptions • Don’t assume automatic rollback on Exceptions! • System Exceptions • Container managed • Rolled back • Bean managed • Marked for rollback • RemoteException, RuntimeException, EJBException, etc. • Application Exceptions • No automatic rollback of transaction • Use @ApplicationException Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 33. #25 Isolation Levels • Configure your isolation levels on your JDBC pools • Glassfish • Use multiple Persistence Units and different pools to access different isolation levels. Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 34. Transaction Isolation Level Description read-uncommitted Dirty reads, non-repeatable reads, and phantom reads can occur. read-committed Dirty reads are prevented; non-repeatable reads and phantom reads can occur. repeatable-read Dirty reads and non-repeatable reads are prevented; phantom reads can occur. serializable Dirty reads, non-repeatable reads and phantom reads are prevented. 1. Dirty Reads: Technically speaking a dirty read happens when a transaction reads some data that is being changed by another transaction which is not committed yet. 2. Non-Repeatable Reads: A Non-Repeatable Read happens when a transaction reads some records that another transaction is modifying. If the reader transaction tries the same query within the same transaction again, the result will be different compared to the first time. 3. Phantom Reads: Occur when we are reading a set of records with a specific WHERE clause and another operation inserts new records matching our WHERE clause. Our transaction has read the records without including the record being inserted. Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 35. #26 XADataSource • Mixing JMS and Database? • Make sure you configure an XADataSource • GlassFish Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 36. #27 EJB Decoupling • Avoid unnecessary coupling • Use CDI Events Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 37. #28 WebSockets • Exposing an EJB as a WebSocket endpoint? • Not well defined in the Java EE 7 specification • Implementation specific at this point • Invoking an EJB from a WebSocket endpoint • Use @Inject or @EJB • Inject into a WebSocket • Similar to JAX-RS • Stateful and SingleBeans are the best match. Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 38. Messaging Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 39. #29 Classic JMS • After 10 years, this classic code can go away! (Arun Gupta) Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 40. #30 JMSContext • Greatly simplied API (Arun Gupta) Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 41. #31 Poison JMS - Redelivery • Message that keeps getting re-delivered! • Use getJMSRedelivered() • Surround with try-catch Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne Wrong type!
  • 42. #32 Poison JMS - Transaction • A JPA rollback will cause a JMS rollback • Use helper method with REQUIRES_NEW Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 43. Interceptors Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 44. #33 Class Interceptors • Simplified AOP interceptors for around-invoke • Invoked before any of the EJB’s methods are invoked • Not called again for internal method calls Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 45. #34 Method Interceptors • Simplified AOP interceptors for around-invoke • Invoked before the specific EJB’s method is invoked Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 46. #35 Default Interceptor • The ejb-jar.xml must be used for a default interceptor • Attaches to all methods of every bean Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 47. #36 Interceptor Ordering • Called from most general to most specific • Default -> Class -> Method • Called in the order they appear in XML or annotations • Use ejb-jar.xml to change ordering Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 48. #37 Interceptor Exclusion • All interceptors are applied • Exclusion by annotation • @ExcludeDefaultInterceptors • @ExcludeClassInterceptors • Exclusion by ejb-jar.xml Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 49. Testing Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 50. #38 Unit Testing • EJB classes are POJOs! • Unit test one class at a time • Mock external resources • JUnit, Mockito • maven-surefire-plugin • *Test.java Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 51. #39 Integration Testing Embedded • Embedded container API • EJBContainer • Mimic (In-memory) resources • JUnit • maven-failsafe-plugin • *IT.java Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 52. #40 Integration Testing Alternative • Alternatives are additional implementations of a Bean • Ignored by CDI when looking for an injection match • Enabled by beans.xml Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 53. Packaging Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 54. #41 EAR • Recall global JNDI lookup names for EJBs java:<namespace>/[app-name]/<module-name> /<bean-name>[!fully-qualified-interface-name] • Default value for [app-name] is EAR file name. • ejbrace-business-ear-1.0.0.0-SNAPSHOT.ear • Use application.xml • <application-name> • <display-name> Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 55. #42 EJB-JAR • Recall global JNDI lookup names for EJBs java:<namespace>/[app-name]/<module-name> /<bean-name>[!fully-qualified-interface-name] • Default value for <module-name> is EJB-JAR file name. • ejbrace-ejb-1.0.0.0-SNAPSHOT.jar • Use ejb-jar.xml • <module-name> Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 56. #43 JPA • Auto-scanning for entities not in the JAR with persistence.xml Core (Entites) Server (EJBs) Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne JavaFX (Client)
  • 57. #44 Override Annotations • The ejb-jar.xml can override certain annotations • Method permission • Annotations allow all roles for testing • Production adds role security ebj-jar.xml glassfish-ejb-jar.xml Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 58. #45 Alternatives Per Environment • The bean.xml can specify alternate beans • Have Maven builds for different environments • Alternatives provide environment specific implementations Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 59. #46 Batching EAR • Don’t have batch jobs on you application stack • Avoid hacks to only get one instance to run • Dedicated batch server • Separate EJB-JAR project • Job XML documents • Readers • Processors • Writers • EAR project • Combines Batch EJB-JAR and application EJB-JAR Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 60. Miscellaneous Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 61. #47 Which Remote Technology? • Remote EJB, JAX-RS, JAX-WS, Servlets • Speed? 75.00 70.00 65.00 60.00 55.00 50.00 45.00 40.00 35.00 30.00 Average call times (ms) 0 2 4 6 8 10 12 Remote EJB (Default Transaction) Remote Servlet Remote JAX-RS Remote JAX-WS Remote EJB (Transaction Never) Remote EJB (Transaction Supports) Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 62. #48 JNDI Lookup • Injection is great • JNDI lookups still needed? • Changing environment properties • Non-container managed classes – new SomeBean() • Multiple resources determined by user input Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 63. #49 Securing RESTful Services • Use tokens with RESTful Web Services (OWASP). • Leverage JASPIC to setup JAAS environment.
  • 64. #50 Timers • Use programmatic or declarative timers? • Programmatic - defined at runtime • Declarative – annotated methods on an EJB • Considerations: • Clustering • Persistence
  • 66. Final Thoughts Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 67. Don’t Panic Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 68. Buy the Book Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne
  • 69. Evangelize • EJB is a super technology Michael Remijan | Ryan Cuprak | https://github.com/mjremijan/JavaOne

Editor's Notes

  1. 21
  2. [Michael] Specify the locking policy on singleton beans. Singleton beans are are unique per JVM. By