EJB learning notes 1, EJB basic knowledge
(1) No state session bean
Do not save the client's session status
Advantages: Use a small amount of instance to meet a large number of customers. Each instance is not identified, and it is equivalent to each other.
Waiting for the stateless session bean: the results and effects of multiple times and one call.
In the cluster, the load balance A machine failed, you can try again on the B machine
Non-et al? Stateless session beans: such as: counter
Can not be switched without failure.
(2) State conversation bean
Save the client's session status
Features: In the bean example in the session state session, the number of people who came to the number of customers is equal to the number of customers, which can simplify programming mode.
WebLogic performs load balancing in the cluster through memory replication technology
Memory Copy Technologies: Each Session Status bean instance will be stored in the memory of the two servers, one server as the primary server, and another as a secondary server.
If the primary failed, the auxiliary change is main, and then the other available servers are automatically selected as auxiliary.
Unfortunately: It is difficult to use a status session bean in Servlet and JSP. It may happen and discovery, generate RemoteException
WebLogic
Synchronize: You can choose to implement Javax.ejb.Sessionsynchronization interface
Afeterbegin () // When entering transaction
BeforeCompletion () / / Before submitting the transaction, write the cached data before commit to the database.
AfterCompletion () // After submitting the transaction, it is used to release statistics on shared resources or update transactions.
Session bean acquires the application of the useertransaction through the getUsertransaction () method in its sessionContext object
Usually sessionContext is stored in a member variable
** Remember to call the setUsertransaction () method before calling the ejb.create () method
Use Begin (), commit (), and rollback () method can be used to use BeGin (), commit ().
(3) entity bean:
It has a master's only identifier
Component: Composed of local interfaces, remote interfaces, bean classes, primary care classes, and configuration descriptors.
Local interface:
Extended Javax.ejb.ejbHome interface, including Create (), Remove (), Finder, and HOME and other methods
1) CREATE () method calls the EJBCREATE () method in the bean class. INSERT method equivalent to data.
2) The remove () method is equivalent to the DELETE operation of the database.
3) The Finder () method allows customers to query and receive references to entity beans that meet the query conditions. Each entity bean must
There is a FindByPrimaryKey () method
4) Home method, similar to stateless session beans.
Subject class:
The entity bean must include a primary care, a primary health class for identifying an entity bean instance, and an entity bean data type must be unique.
The main health class can be the basic type of Java String Integer or a user-defined.
It is also possible to be a multi-field spiritual composite maintenance.
BEAN class and beans context environment:
Implement the Javax.ejb.ejbobject interface, which contains the syntax format definition of the business method.
The bean class implements the javax.ejb.entityBean interface, like the Javax.ejb.SessionBean interface, the ENTITYBEAN interface contains the syntax format of the EJB container calling the bean instance.
After executing the bean's constructor, call the setentityContext () method immediately, and pass the entryContext of the bean instance to it.
The Bean class implements the business method in the Home method and remote interface. The Home method is a method for anonymous instances should not use the relevant key value.
Divided into:
Container-Managerd Persistence (Container-Managerd Persistence) CMP
Features: The EJB container is automatically generated for writing the data of the entity bean into the database.
Advantages: Bean authors can avoid writing code in real BEAN and relational database data access. CMP will automatically handle this process.
Personality: Each CMP entity bean has a set of container managed fields, which are stored in the database and can be loaded. Usually, each container management field corresponds to
A column in the relational database.
Each field of the container management must be defined in EJB-JAR.XML, which enables the container to match the fields managed by the container to the SET and GET methods in the bean class.
In addition, Bean authors can add another CMP configuration description file WebLogic-Cmp-Rdbms.xml, which contains the word table name and the word management of each container.
Segment and the corresponding data column mapping.
Bean Management Persistence (Bean-Managerd Persistence) BMP
Features: In the BMP entity, Bean authors need to write database access code, which is written JDBC code, insert, delete, and query entity bean data in the database.
Advantages: Allows the author of Bean to completely flexibly process entity beans's persistent data, because the author needs to write data accessed, he can use any persistent storage.
The standard relationship mapping between the EJB2.0 CMP provides entity beans to automatically manage interactions between business objects.
CMP has more access control, so CMP has better performance than BMP.
(4) Message Bean
JMS and EJB successfully combined together, integrated results
Features: The client does not need to call the message bean opposite: the client only needs to send a message to the JMS purpose.
After the message arrives, the message bean's onMessage () method will be called to process this message.
The message bean is used to perform an asynchronous operation in the server.
2. EJB composition
(1) Remote interface
Public Interface HelloWord Extents EJBOBJECT
{
// EJBObject interface method
Ejbhome getejbhome () throws remoteexception;
Object getprimarykey () throws remoteException;
Void Remove () throws RemoteException, RemoveException;
Handle getHandle () throws remoteexception;
Boolean isidentical (EJBOBJECT EJBOBJECT) THROWS RemoteException;
}
(2) Local interface
The local interface is an EJB factory, and the client can create, find and delete the EJB instance using the local interface. Just write the syntax call format of the method in the local interface
Public Class HelloWorldHome Extends EJBHOME
{
// EJBHOME interface method
Void Remove (Handle Handle) Throws RemoteException, RemoveException; Void Remove (Object O) THROWS RemoteException, RemoveException;
Ejbmetadata getejbmetadata () throws remoteException;
HOMEHANDLE GETHOMEHANDLE () THROWS RemoteException;
// HOME
Public HelloWorld Create () THROWS CREATEXCEPTION, RemoteException;
}
(3) Bean class
Public Class HelloWorldBean Implements SessionBean
{
// sessionbean method
Public void setsessionContext (sessionContext sessioncontext)
/ ** Adjust the second method meeting end * /
Public void ejbremove ()
// EJB manages a group of stateful session bean instances in work through the mechanism of standing and activity
/**activity*/
Public void ejbactivate ()
/ ** standby * /
Public void ejbpassiVate ()
// bean class
// Each home Create method corresponds to an ejbcreate () method
// There are many different versions of the CREATE () method. And the Create method must have an ejbcreate () method corresponding to one
Public void ejbcreate ()
}
Do not implement remote interfaces in the EJB class
3. EJB Configuration Descriptor
(1) EJB-jar.xml
session>
enterprise-beans>
Method>
container-transaction>
ejb-jar>
(2) WebLogic-Ejb-jar.xml (note)
WebLogic-Enterprise-Bean>
WebLogic-EJB-JAR>
4. Establish an EJB file file
COM / DHC / HELLOWORLD / (Package)
COM / DHC / HELLOWORLD / HELLOWORLD (Remote Interface)
COM / DHC / HELLOWORLD / HELLOWORLDHOME (local interface)
COM / DHC / HELLOWORLD / HELLOWORLDBEAN (Bean class)
META-INF
META-INF / EJB-JAR.XML (Configuration Descriptor)
Meta-inf / weblogic-ejb-jar.xml (WebLogic Server Configuration Descriptor)
Description: Meta-INF must be uppercase
5. Container management transaction
Nerver: Do not participate in the transaction, if you participate in the generation of RemoteException
NotSupported: Cannot participate
Supports: If the caller is participating in the transaction, the corresponding EJB call can also participate in the transaction, otherwise it must not
Mandatory If the caller has a transaction, the corresponding EJB can participate in the transaction, otherwise TransactionRequiredException
Required If the caller has a transaction, the corresponding EJB can participate in the transaction, otherwise, the container will start a transaction before calling the corresponding EJB.
This transaction is submitted after the method call is completed.
RequiresNew starts a new transaction before calling the corresponding EJB, which is submitted to this transaction when the method calls returns.
6, EJB reference
Ejb-jar.xml
EJB-REF>
WebLogic-ejb-jar.xml
ejb-reason-description>
program
Content CTX = New InitialContent ();
Object h = ctx.lookup ("java: / comp / ENV / EJB"); // Environment variable is read-only, and is current EJB local variables.
WidgetHome Home = (widgetHome) PortableRemoteObject.narrow (h, widgehome.class);
7. Resource Manager's reference
Define references to resource management
Example: Establish a mapping of JDBC, DBPOOL and JDBC data sources
Ejb-jar.xml
resource-ref>
WebLogic-ejb-jar.xml
resource-description>
Config.xml
Name = "dbpool" Targets = "MyServer" JDDINAME = "dbpool" (JNDI name) PoolName = "developportpool" /> Advantages of reference We use a lot of mapping and configuration to establish a reference to the resource manager, but it is still worth it. Think so that the deployment personnel are reconfigured and do not need to modify the actual Bean class code. Even no need to modify the configuration descriptor of EJB Java bean code Content CTX = New InitialContent (); DataSource DataSource = (Datasource) CTX.lookup ("Java: / Comp / Env / JDBC / DBPOOL"); 8. Handle: As a serialized object, sufficient information is packaged in the handle to reconstruct the reference to EJBOBJECT. The handle can be used to deliver references to EJBOBJECT in two processes that cooperate. Accept the process to get the reference to EJBObject from the handle. In order to get the handle, you can call the getHandle () method of the EJBOBJECT interface, return a handle instance In order to reconstruct the EJBObject reference. You can use the getEJBObject () method of the HANDLE interface. example: HelloWorld HW = home.create (); Javax.ejb.handle Handle = hw.gethandle (); HelloWorld HelloWorld = (HelloWorld) PortableRemoteObject.Narrow (HLLOWORLD.CLASS); HOMEHANDLE: Similar to Handle, but cannot reference EJBOBJECT HOMEHANDLE contains enough information to reconstruct ejbhome () references. difference: Is calling gethomehandle () method and getjbhome () method Example pieces: Content ctx = new initialContext (); Object h = ctx.lookup ("HelloWorldejb"); HelloWorldHome Home = (HelloWorldHome) PortableRemoteObject.narrow (H, HelloWorldHome.Class); HOMEHANDLE HOMEHANDLE = home.getHomeHandle (); Object nh = homehandle.getejbhome (); HelloWorldHome NewHomeReference = (HelloWorldHome) PortableRemoteObject.narrow (NH, HelloWorldHome.Class); advantage: They can automatically store the information needed to rebuild the reference 9. Tips for using transactions: (1) A business does not involve too much operation. (2) Container management and BEAN management transactions The transaction costs the resources in the application server, and it costs the database resources, so the shorter transactions, the better. Try to use the container management transaction without adopting a bean management transaction. (3) EJB encounters an error, you need to force the transaction to roll back. Use ejbobject.setrollbackOnly (); (4) Can't let the transaction involve the web layer and representation logic (5) Enterprise applications should not be used to elected the Supports transaction property, because only the caller starts a transaction, EJB can run in the transaction.