Demo with database and EJB resource injection Netbeans includes the Java DB database engine (also called Derby, a name which appears in some places, also previously called [IBM] Cloudspace). This is a somewhat complete implementation of an SQL database implemented solely in Java. As such, it is portable and quite lightweight. Other more complete database servers include open-source (or hybrid-licensed) ones like MySQL, PostgreSQL, and commercial ones like the main Oracle database product, Microsoft SQL Server etc. All of these have good JDBC-driver. JavaDB is fully integrated in the Netbeans package and therefore very convenient to use for prototyping. However, you can connect to other databases using the same tools. Creating a database You first need to create a database instance before you can connect to it. Bring up the Services view in Netbeans (Windows -> Services). Expand Databases. Right-click Java DB, select Start Server. Right-click again and select Create Database. Choose a database name and user credentials, in later parts of the examples we assume this is mydb and test/test. Connecting to the database The specific database instance should now have appeared as another node under Databases, i.e jdbc:derby://localhost:1527/databasename Right-click to Connect. You will now see a tree with the user name as a node. Each user can have a separate schema in a database, but you naturally tend to want to use the same schema for all users. This is beyond the scope of the course. Note that database-level users do not need to map to user identities in the web application. Right-click on the jdbc:derby… node again and choose Execute Command. Let’s create the PERSON table from the lecture examples. Write the following: CREATE TABLE PERSON(pnr int, firstname char(10), lastname char(10), age int); ALTER TABLE Person ALTER COLUMN Pnr NOT NULL; ALTER TABLE Person ADD PRIMARY KEY (Pnr) Creating a data source You’ll need to add a data source in your application. Go to the Projects view again. Right-click on your application. Select New -> Other. Select GlassFish.Select JDBC Resource. Within this wizard, you’ll also choose to create a new Connection Pool, and then select the existing database to connect to (which you established in Services). A file sun-resources.xml will be added to your project. Now, things are a bit complex. You will have a name for your database within Java DB. You will have a name for your connection pool, that controls connections to this database. You will have a name for your data source. Mapping your data source You also need to map that data source name into a JNDI resource name. For GlassFish, this is done in sun-web.xml (or equivalently glassfish-web.xml), see for example http://docs.oracle.com/cd/E18930_01/html/821-2418/beaoa.html You can create a new GlassFish Deployment Descriptor in Netbeans and then add a resource-ref tag with the elements res-ref-name and jndi-name. If you use the same new throughout, this could look like: <resource-ref> <res-ref-name>jdbc/mydbsource</res-ref-name> <jndi-name>jdbc/mydbsource</jndi-name> </resource-ref> Injecting your data source You can reference your data source like this in your servlet: @Resource(name="jdbc/mydbsource") // The name you entered for the JDBC resource private DataSource dataSource; Note an oddity here. If you put the wrong name in, youre Resource annotation will default to finding the default javax.sql.DataSource binding. This will be the default sample database. You will not get a very clear warning of this. The fallback structure for injection can both be very good and somewhat dangerous. You can easily have several levels of flexibility. Creating your EJB In real life, you will tend to separate your EJBs from your web application. However, for a small application, you can have them all together. You create a session EJB by creating a local interface (tagged with the @Local annotation), implementing it in another class with @Stateless, @Singleton or @Stateful annotation. In the example, you find the getSecretWord method. Injecting your EJB Your EJB can be injected into a servlet by creating a variable of the local interface type and annotating it with the @EJB annotation. See the example. Asynchronous EJB operation The demo servlet looks at the parameter name async. If that is used, asynchronous EJB calls are made. If not, synchronous calls are made. When you load the page, you might note that GlassFish seems to use a worker pool of 16 workers, so in asynchronous operation you will see 16 lines appearing at a time, more or less. Create JPA mapping You can create a JPA mapping of the Person table automatically in Netbeans by using New -> Persistence -> Entity Classes from Database. This auto-generated class might not be the most beautiful code, though. The persistence context (of type Entitymanager) is injected into the servlet using @PersistenceContext.
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project