J2EE - How to solve automatic growth key values in JBOSS
[Foreword]
The automatic growth key value is a significant feature of the relational database. For example, MS SQL Server, MySQL can directly set a field to auto-increment type, Oracle also provides a similar Sequence Number feature. However, before the EJB2.0 specification, the CMP section did not have a related statement on the automatic growth keys, which has been deeply affected by J2EE developers, and the application server developer also proposed their own solutions, but in These solutions are unfair, but also to lack versatility, such as WEBLOGICs, known as famous rows, is only supported by some mainstream databases when 1.0 specification. The version 3.0 version of JBoss does not support the Auto-Increment feature, and it is formally supported to version 3.2. This article introduces how JBoss uses Autonumber this EJB plugin in version 3.0 to implement the database table key value automatic growth. Introduce how to use "Unknown Keys" in version 3.2 to truly implement the automatic key value growth in the second half of the article. . The background database is used by MySQL4.0.12.
EJB is a technique that is still rapidly developing, which means changes, and "fast" is also suitable for "change" two words, especially in the CMP portion of EJB, and its changes are particularly prominent. CMP is the most essential embodying of EJB. Due to the way of using an object, most of the database still has a non-negligible difference between the relationship type, expression and expression, and thus the EJB specification needs to be considered. Related factors have to make a balanced hierarchy. Due to the development of the previously mentioned, the final maturity of the specification is not one, a standard launch may be postponed by the timing factor (technology or business), and developers should be able to recognize this in actual development. Where the limitations caused by the regulations, the subjective initiative is given to a new realm in consideration of problem solving problems.
Autonumber handling method provided by JBoss3.0
JBoss3.0 provides users with an Autonumber plugin to "Pseudo" implementation of the key value of the CMP, which is crowned to a "pseudo" word because this function is not implemented by the database itself, but uses it. A factory type CMP Entity bean generates a corresponding key value for a variety of entity beans (tables in the database), users do not have to consider how to maintain the uniqueness and continuity of key values, all of which are proxy by autonumber, user What to do is to deploy this bean jar before using this "plugin", and figure out how the call principle is known to use this CMP bean.
Before deploying this plugin, let's take a look at the code annotation of the JBoss source code file AutonumberFactory:
/ **
* Gets the next key for the given collection.
* Note 1: You Must Deploy EJB Autonumber
* Note 2: The Keys Are Persistent in Your Database, Independent of
* The Actual Table
* Note 3: You can only add instances to the collection Which Have A
* Key generated by this method, Otherwise the keys are not guaranteed
* To be unique
* Note 4: Key Values Are> = 0
* /
Purdantly: This class is to get the next key value to a key collection. In order to use this factory method, you must first deploy autonumr this EJB, the key set information is stored in the database, not related to the actual table, to ensure the key value Uniqueness, you can only generate key values through this factory method, and the range of key values is integer greater than or equal to 0. Deploy Autonumber CMP Entity Bean
The location of the Autonumber plug-in is in% JBoss_Home% / Server / Default / lib / autonumber-plugin.jar, what is surprising is that JBoss does not give an example or guided instruction file (or I have not yet Find). After studying the relevant source code, the following is an deployed instance, and how to create a table in the database correspond to this entity bean.
First create a table in MySQL to correspond to Autonumber Bean. This table is simple, only two field columns Name and VALUE, the Name column is used to store key value names, and value is used to store the currently available key value. The DDL statement is as follows:
Create Table `Myauto` (
`Name` Varchar (20) Not null default ',
`Value` int (11) Not null default '0',
Primary key (`name`),
UNIQUE Key `Name` (` Name`)
) Type = InnoDB
Here will set the Name as the primary key, and to Unique, the key name is not repetitive.
EJB-JAR.XML Description File:
CMP-FIELD>
CMP-FIELD>
Entity> Enterprise-Beans>
Method>
container-transaction>
askSEMBLY-Descriptor>
ejb-jar>
JBossCMP-JDBC.XML Description File:
CMP-FIELD>
CMP-FIELD>
Entity>
enterprise-beans>
jbosscmp-jdbc>
JBoss.xml Description File:
Entity>
enterprise-beans>
jboss>
Before formal deployment, place the deployment file into a meta-inf directory, and then enter a new JAR file with the autonumber-plugin.jar file, and finally the new JAR file COPY to% JBoss_Home% / Server / The Default / Deploy directory and start JBoss to see the deployment.
AUTONUMBER This CMP bean is called through the static method of the factory class of AutonumberFactory. If you have a table called Table1, there is an integer type of column acts as a primary key, then you can value this key value. The collection is named "Table1_PK", which can get the key value of the current INTEGER type of Table 1 through AutonumberFactory. GetNextinteger ("Table1_PK". If it is the first call, then the value is 0, and Autonumber is called by this factory class. You don't have to do any initialization work, you want to write such an Import statement in the called CMP class file: import Org.jboss.varia.Autonumber. Autonumberfactory;
In addition, Autonumber also provides key-value initialization and reset methods, and refer to the source code file of AutonumberFactory. AutonumberFactory is mainly used in the EJBCREATE method of CMP Bean, such as:
Integer mypk = autonumberFactory. Getnextinteger ("Table1_PK");
SETID (MYPK);
The principle of autonumber
The core of the Autonumber plugin is a CMP bean, and the factory is used to generate an automatic growth button, but this plant mode is built on the database, which can generate key values for multiple tables at the same time. Every time you call getNextinteger, AutonumberFactory obtains the local interface of Autonumber Bean through the JNDI name, calls the method provided by the bean. The method is to return to the key value in the current database, and then add the key value to one.
summary
Autonumber is equivalent to opening another way to facilitate the user to get a key value of an integer type and make sure that the key value acquired by this path can remain unique. However, this way of winding the database has a big problem, because it is not a database itself, such as you add a record to a table in another data access program (such as the client provided by MySQL), if you forget The form of simulation autonumber gets the key value, that is, forget to add the corresponding key value in the MyAuto table, then your EJB will have a conflict. Another disadvantage is that the key value only supports the Integer type. For the first shortcomings, Autonumber cannot be solved, but the second shortcomings are solved, that is, create our own Factory method, support the various needed primary key data types, and Autonumber gave us a good design. example.
Automatic growth key value function in jboss3.2
prerequisites
For Mysql users, you need a better engine to support better features, in order to let 3.2 versions can complete this functionality in MySQL, you must first download an advanced JDBC engine,
MySQL Connector / J 3, the engine supports JDBC3.0, as for the reasons to see the [Deployment Description File] section. Here is a topic here, if you are a good Java programmer, then you must always maintain a high degree of sensitivity to the version, see the JBuilder version update, JBoss4.0 is also in the agenda, higher The version means better features, but don't miss the difference between the versions, not only the difference between the main body, but also the difference in the contact (here is the main body, MySQL JDBC Driver is the contact body), otherwise you will The head is in the middle of the wall, "" For the Iraqi people ", Yunyun. Gossip less, copy the driver JAR file of Connector / J 3 to% jboss_home / server / default / lib / below, remember to remove the original drive. A CMP bean scene
For convenience of how to describe, a simple CMP bean is used as an example. Suppose there is a table auto_inc_test, only two field IDs and name, where ID is automatic growth type, SQL script is as follows:
CREATE TABLE AUTO_INC_TEST
ID tinyint (4) Not null auto_increment,
Name varchar (10),
Primary Key (ID)
) Type = InnoDB
As for how to design and implement this CMP bean, this is not described herein. For JBuilder, it is just a few Drag and DROP rounds, we will focus on the configuration of the deployment file. Our bean name is called TestAutoincbean, with an EJBCREATE method, only one character type data is parameter, namely the Name field, ID, and Name fields have a GET / SET method in the bean.
Deployment description file
JBoss3.2 has a very different differences over version 3.0, one is support for EJB2.0 specification, which is reflected in the deployment documentation. In three description files ejb-jar.xml, jboss.xml, and jbosscmp-jdbc.xml, the first two and general deployments are not different, and the last deployment file is described below:
JBossCMP-JDBC.XML Description File
read-ahead>
unknown-pk>
defaults>
CMP-FIELD>
CMP-FIELD>
Entity>
enterprise-beans>
dependent-value-classes>
jbosscmp-jdbc>
Here, the description portion of the database map is placed outside the
Through the above two solutions in the JBoss version of the AUTO-INCREMENT issue, some of J2EE's characteristics can be seen that these features are quite important for developers. J2EE's design ideas are very advanced, but it also has a certain complexity, and there is a corresponding increase in the problem that needs to be considered and dealt.
JBoss is an development source and completely free app servers. Its match with MySQL can be described in "a pair of natural, and has a pair". For developers who need practice, it is a big gospel, not only can Free to develop above, you can also refer to its design idea (such as the design analysis of AutonumberFactory). JBoss reveals two words in using it, it is "simple", but it is not a powerful J2EE engine function.