`
leng_cn
  • 浏览: 301882 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate used with tomcat 5.5.x JNDI(来源于http://www.theserv)

阅读更多

Part 1: JNDI Setup for Tomcat 5.5.x

There have been a number of changes in Tomcat configuration as the Tomcat version advanced from Tomcat 5.0 through version 5.5.x. These changes have affected Tomcat JNDI setup, which in turn has affected Hibernate, assuming you wish your Hibernate installation to take advantage of the DataSource facility made available from Tomcat JNDI.

In addition, as the Hibernate version advanced from 3.0 through 3.1, there was a significant simplification (and therefore a change) in how you obtain a thread-safe Hibernate session.

Tomcat is a hybrid sort of environment (neither fish nor fowl) from the standpoint of Hibernate. Tomcat provides a JNDI and DataSource, unlike a standalone application. The DataSource facility should facilitate portability among application servers. But like a standalone environment, Tomcat provides no Transaction Manager. Hence your code must use the Hibernate Transaction Manager, as it would in a standalone application environment.

We found it difficult to obtain any reasonably complete description of this pair of component versions being used together - we could not find the information on the web, not even on the Hibernate.org site (http://www.hibernate.org). In several places we found older information, but it no longer seems very useful because of the changes that have taken place. The Hibernate 3.0 Reference Documentation opened with a very useful chapter on Hibernate/Tomcat integration (based on Tomcat 4.1), but that chapter has been removed from the current Hibernate 3.1 Reference Documentation. We hope this set of three articles will help you to use the current versions of Hibernate and Tomcat on your project.

The Context.xml File

In earlier versons of Tomcat, you configured Tomcat JNDI via the server-wide configuration file, server.xml. You included multiple <resourceparams></resourceparams>elements within this file, one <resourceparams></resourceparams>element per DataSource. In Tomcat 5.5, you no longer use this server-wide file to configure JNDI. Instead, you place a per-application file named (exactly) Context.xml into your META-INF directory, for instance C:\Tomcat 5.5\webapps\BasicWeb\META-INF\ Context.xml. (You may find references on the web indicating this file should be named after your application, for instance YourApp.xml. That is not correct - you should use the unvarying filename Context.xml.)

Here is a portion of a screenshot showing the placement of Context.xml in your Tomcat directory hierarchy:

In your application's Context.xml file, you should include one <resource></resource>element per DataSource. This <resource></resource>element has a set of attributes defining the DataSource, but no sub-element. Here is the complete text of Context.xml for an application which only requires a single DataSource (certainly not an unusual condition):

 

The above <resource></resource>element has the effect of creating a Tomcat JNDI DataSource entry. The JNDI and this JNDI entry are created when the Tomcat server is started. This JNDI DataSource entry is no different from such an entry in an application server from the standpoint of retrieving information from the JNDI. (However, the Tomcat JNDI is read-only after the Tomcat startup cycle is completed. Neither your custom application nor Hibernate can add or revise entries in this JNDI during ongoing Tomcat operation.)

Incidentally, the several articles in this series happen to be using MySQL as the datastore, as you can see in the above <resource></resource>element.

Not only can Hibernate use the Tomcat JNDI, but your raw JDBC can access the JNDI as well, in the same manner as you can access a DataSource from a JNDI in an application server. Here are five lines of code from a servlet which obtains a JNDI connection from the above JNDI <resource></resource>element, and employs raw JDBC (not Hibernate) to perform queries against the database.

 

We would like to present the full text of the servlet, but before we do so, it may be worth stating our objective in this example of running code and in all the examples of running code that follow: Our objective is to teach and illustrate how Hibernate 3.1 and Tomcat 5.5 function together (which is after all the title of this series). Code which shows its innards because it is meant to be easy to understand, like this code, is not the same as commercial quality code you intend to deploy - which would include complete exception handling and other practices such as use of the MVC pattern which are irrelevant to our purposes. (You would certainly use a JSP, not a servlet, as your View component. But introducing even part of an MVC pattern would blur the focus from where we wish it to stay - the intersection of Hibernate and Tomcat.)

Here, then, is the complete text of our first servlet example:

 

Note that Tomcat, like a normal application server, is actually providing database connection pool support without the servlet having to ask for such support or even knowing it exists. In the case of Tomcat, the connection pool support is provided by the DBCP component. In the <resource></resource>xml element shown earlier, the maxActive="100" attribute signified a maximum of 100 active connections should be supported in the pool.

This completes the first article in this series, which has focused on Tomcat 5.5.x JNDI setup. The Context.xml file shown above, without a single character needing to be changed, will also support Hibernate. In fact, such a file is absolutely necessary in order to utilize Tomcat JNDI from Hibernate.

In the next article, we will document Hibernate 3.1 configuration under Tomcat 5.5.x, and show a simple Hibernate-based servlet which uses the above Context.xml and JNDI setup. The Hibernate-based servlet in this article will perform queries against the MySQL database.

In the third article in the series, we will perform inserts into the database within the boundaries of a Hibernate transaction, again using the above JNDI setup.

Part 2: Tomcat Servlets that Employ Hibernate for Database Queries

This is the second article in a three-article series describing the configuration of Tomcat 5.5.x and Hibernate 3.1 so that they can be used with one another. In the first article, we described how you configure the Tomcat 5.5 Context.xml file, which sets up the Tomcat 5.5 readonly JNDI. This JNDI makes DataSources available to your Tomcat servlets, regardless of whether your servlets employ Hibernate or raw JDBC. In that first article, the example servlet employed raw JDBC to access our MySQL database.

 

In this second article, the example servlet will use Hibernate, not raw JDBC, to access the MySQL database, but the same Context.xml file will continue to be used, without change. Hence Hibernate will take advantage of the same JNDI DataSource as the raw JDBC used in the previous article.

Web.xml

Let's begin by looking at web.xml. (We assume you know that web.xml identifies and configures the components of any Java web application, and that you understand the format of this file.)

 

The above represents the entire contents of web.xml for this three-part series. It contains three servlets:

  • Retrieval - the servlet which employed raw JDBC in the previous article.
  • RetrieveViaHibernate - the servlet in the present article which queries the same MySQL database as the first article, but now uses Hibernate.
  • InsertViaHibernate - a servlet in the third and final article which performs inserts into the database within the boundaries of a transaction obtained from Hibernate.

In addition, near the top of web.xml is an xml listener element, which in this case defines a ServletContextListener. The listener is a class that will be instantiated when the web application is being readied to process requests (for instance at Tomcat server startup). Specifically, the contextInitialized() method of this class will be called to do whatever we want it to do before any request is processed by this application. For instance, before any of the three servlets shown in this web.xml is called for the first time, contextInitialized() will be fired for our application. This method will not be fired again unless and until the application is reloaded, or Tomcat is re-started.

ServletContextListener

In our case, what we want the ServletContextListener to do is to load our HibernateUtil class, so that the latter will instantiate a static Hibernate session factory. As a result, the Hibernate session factory will already be available by the time Http requests are received by any of our three servlets. Let's look at the code for our designated listener, tomcatJndi.HibernateAppListener:

 

Note the Class.forName(...).newInstance() method call above, which seeks to ensure the designated class (tomcatJndi.HibernateUtil) is loaded, hence that the static initializer in HibernateUtil is invoked. We need to look at HibernateUtil.

HibernateUtil

 

Note above where the static SessionFactory (a singleton) is instantiated by buildSessionFactory(). The interface org.hibernate.SessionFactory represents a Hibernate factory from which our code can obtain org.hibernate.Session objects. The Hibernate Session is the vehicle by which our applications can interact with Hibernate, and ask it to perform any of its functions.

It should be noted the class HibernateUtil is simply copied from the Hibernate.org web site (http://www.hibernate.org). Previous versions of the HibernateUtil class available from that site (with earlier versions of Hibernate) were more complex, incorporating a ThreadLocal instance. On the current Hibernate.org site, you can find the document "Hibernate Sessions and Transactions" ( http://www.hibernate.org/42.html), which includes the following wording:

Note: There are many variations of much more complex HibernateUtil classes floating around the net. However, for Hibernate 3.1, the code shown above is the only code that is needed. Every other HibernateUtil is obsolete for Hibernate 3.1.

Let's say your servlet is limited to some kind of database query, i.e. it doesn't update the database in any way. Then it could utilize the Hibernate SessionFactory obtained from HibernateUtil in the following way:

 

Note even a simple query utilizing SessionFactory.openSession() should use a Hibernate transaction, and commit the transaction, according to the Hibernate reference documentation. We are choosing to follow this recommendation - it certainly hurts nothing.

Say you have another servlet which employs Hibernate to perform some kind of update to the database. Then your code might take the following form (basically identical to the above):

 

Hibernate.cfg.xml

The method call org.hibernate.cfg.Configuration().configure() in HibernateUtil.java looks for the Hibernate configuration file hibernate.cfg.xml in the classpath. So we must look at that file next. It should be placed in the Tomcat application's classpath in the WEB-INF/classes directory or in a subdirectory of this directory. Here is a screenshot showing the placement of hibernate.cfg.xml (alongside the log4j.properties file, which also needs to be in the classpath).

And here is the full text of the Hibernate configuration file that we are using in this article series.

 

We need to comment not only about what is in this configuration file, but also about certain things that are not in the file. The first thing to notice in this file is <property name="connection.datasource">. This is where Hibernate is directed to utilize the existing JNDI datasource which we configured in Context.xml in the first article. If we were not using the JNDI entry, we would need entries in hibernate.cfg.xml similar to the following (and the above connection.datasource entry would need to be removed from the file): </property>

 

Next, notice the entry <property name="current_session_context_class">is set to thread. This entry basically allows the new, simplified version of HibernateUtil shown above to be used, so that we don't have to maintain our own ThreadLocal to provide thread safety to our Hibernate Session object. </property>

The first item we wish to point out that you don't see in this file is an xml attribute rather than a sub-element. Specifically, you don't see the name attribute on the session-factory element. What you don't see is anything like the following:

<session-factory name="java:/hibernate/HibernateSessionFactory"></session-factory>

If the name attribute were present - as you would expect it to be in an application server environment - then, when the static initializer in HibernateUtil invoked BuildSessionFactory() in this line of code shown earlier:

sessionFactory = new Configuration().configure().buildSessionFactory();

what would happen is that Hibernate would seek to install the session factory entry into JNDI. This is fine in an application server, but not in the Tomcat readonly version of JNDI. (An exception would be thrown.) In an application server, there would probably be a second change to the version of HibernateUtil shown earlier, namely, the getSessionfactory() method would retrieve the SessionFactory reference from JNDI, rather than from the static variable which it uses in the version we have shown.

Here are several other properties you don't see in this configuration file:

  • transaction.factory_class - the default value of JDBCTransactionFactory is automatically being applied to this configuration. If you wished, you could include the transaction.factory_class property, explicitly giving it the (default) value JDBCTransactionFactory. In an application server environment you could include this property, giving it the value JTATransactionFactory instead. You cannot normally assign the value JTATransactionFactory with Tomcat (unless you used a third party JTA implementation library which is not distributed as part of Tomcat).
  • jta.UserTransaction - in an application server environment, you would normally set this to java:comp/UserTransaction, enabling JNDI lookup of a UserTransaction. For the reason stated above, you cannot do this with Tomcat.
  • transaction.manager_lookup_class - in an application server environment, this could explicitly identify the provider of the JTA implementation. Again, this would not normally be useful with Tomcat.

Incidentally, simply placing the jar file jta.jar into your Tomcat context classpath does not give you JTA transaction support. It gives you the definition of the interface UserTransaction, but not an implementation which conforms to this interface. The implementations are available from the application servers, or from a third party open source implementation such as JOTM (but this JTA implementation is not distributed with Tomcat).

Hibernate Mapping Files

In the configuration file Hibernate.cfg.xml shown above, two mapping files are listed:

  • tomcatJndi/Car.hbm.xml
  • tomcatJndi/Driver.hbm.xml

These mapping files must be present in the application's classpath, in the indicated package directory under WEB-INF/classes, as shown in the following screenshot:

As you can see, a particular mapping file is expected to be in the same directory as the corresponding Java class, such as Car.hbm.xml and Car.class.

Before we can show you the contents of our pair of mapping files, we need to explain our very simple data model and application. Our application is concerned with a small family - a husband, Jack; a wife, Jill; and their son, James - who own four cars among them. The reason they own four cars is that one of the cars, a sporty car owned by James, is often in the shop for repairs. James has a tendency to have accidents for which others - never he - is responsible, and the accidents amazingly tend to occur when he is driving that specific car. Note that no member of this family is permitted to drive each other's cars - the attachment to their own car is too great. As a result, there is a one-to-many association between a Driver and the Cars that driver may drive. (In most families, the association would probably be many-to-many, but in the family in our example it is one-to-many.) Nothing stops this family from conceivably purchasing additional cars, but due to the prevailing family dynamics in their family, no member will ever drive each other's cars.

The result of the above data model is that the Driver.hbm.xml file must reflect the collection of cars a driver may potentially drive. There is no similar need for a collection in Car.hbm.xml. Thus Driver.hbm.xml will be the slightly more complex of the two mappng files. Let's look at the simpler file, Car.hbm.xml, first.

 

Let's look at the corresponding Java source file:

 

And here is the MySQL create table statement. Each car record must contain the foreign key which points to the owner/driver:

 

As stated above, the Driver.hbm.xml mapping file (and corresponding Java source file Driver.java) is slightly more complex, due to the requirement for the one-to-many mapping to the set of Cars the particular Driver owns and drives.

 

The element in the above file represents the Java collection type, set. This xml element and its subelements specify that the table car contains a foreign key column, fk_driver_id, which references the Driver who is on the one side of the one-to-many relationship.

Here is the corresponding Java source file:

 

And here is the MySQL create table statement:

 

Hence we have set up the required one-to-many relationship between a Driver and the Cars driven by the Driver.

Finally, here is the example servlet which uses Hibernate to query this pair of tables:

 

Note above where this servlet obtains the current Session from the SessionFactory in HibernateUtil, and begins a Hibernate transaction, and near the end of the routine, where it commits the Hibernate transaction, even though only a query (not a database update) is being performed.

There are a couple of object-oriented functions which Hibernate is performing in this servlet example. First, observe that unlike the raw JDBC in the first article, the Hibernate database query above is actually returning instances of the Driver and Car classes, as defined in Driver.java and Car.java and the corresponding hbm.xml mapping files. The above code is not returning raw ResultSets.

Second, take a look at the query in the above code: "from Driver driver" (the "select *" syntax is assumed, and not needed). This query says nothing about the Car table, but the applicable Cars are being returned. Actually, the word "Driver" in this query doesn't refer to the Driver table - it refers to the Driver Java class, which does include a Collection of the Cars owned and driven by a given Driver. Hence the query directly returns the Java object instances you wish to work with - there is no need to convert from ResultSets to the target objects. Moreover, the reference from one Java object type (Driver) to the referenced collection of Java objects (Car objects) is pre-populated for you.

There is one further point we wish to make about the above servlet. Notice the following line of code near the top of the method doQuery():

 

We could have chosen the following alternative syntax - but we would never choose to do so with Tomcat:

 

Why would we never use this alternative syntax with Tomcat? The difference between openSession() and getCurrentSession() is that the former, each time it is used, provides a brand new Hibernate Session. That is exactly what we want in our servlet. In contrast, getCurrentSession attempts to associate a Hibernate Session with a specific thread (the Singleton-per-Thread pattern), which Hibernate achieves via the use of an embedded (hidden) ThreadLocal. Unfortunately, Tomcat maintains a thread pool, and re-uses a given thread after a particular Http request is finished with it. Hence a brand new Http request can receive a previously used thread, which already happens to have a Hibernate Session associated with it (via the ThreadLocal), and getCurrentSession() may by chance receive an unrelated Hibernate Session when it ought to receive a brand new one. We may have a new Http session, and logically a new thread, but physically be re-using an existing thread. In this way, Tomcat 5.5.x and Hibernate 3.1 can confuse each other.

We choose to bypass the issue entirely by using SessionFactory.openSession, and avoiding the use of SessionFactory.getCurrentSession in a Tomcat environment.

The conflict we described above is readily testable. Setup a Tomcat 5.5 environment allowing only a small number of concurrent threads - say 3 or 4. (You accomplish this via an entry in Tomcat Root\conf\server.xml, namely by setting the maxThreads attribute of the applicable <connector></connector>element in this file to 3 or 4.) Create a couple of distinct, simplistic business transactions (conversational transactions or long-running transactions) which span Http requests. Attempt to preserve information in a ThreadLocal - any information –it doesn’t need to have anything to do with Hibernate - and do some tracing/logging in which you display the thread ID. You will see Tomcat thread pooling eventually recycle thread IDs to another business transaction, allowing inappropriate access to the ThreadLocal contents to take place.

You may wish to do some research on the ThreadLocal facility.

That concludes this fairly lengthy second article. It has shown you how to configure Tomcat 5.5 and Hibernate 3.1 for use with each other, and how to perform object-oriented queries in this environment.

In the third article we will show you how to invoke database updates in this environment, and we will have something to say about multi-Http-Request business transactions - the Shopping Cart scenario.

Part 3: Hibernate for Database Updates, and the Shopping Cart Scenario

This is the third in a three-article series concerned with the configuration of Tomcat 5.5.x and Hibernate 3.1 so that these component versions can be used together. At the present time, much of the information available on the Web is unfortunately focused on earlier versions of these components, when their configuration and programming were somewhat different than at present.

In addition, the use of the Hibernate session object has changed somewhat in a manner that definitely affects its use with Tomcat (as we will show shortly), and it is important to be aware of how it works now.

A Brief Synopsis

In the previous article, the example servlet used Hibernate, not raw JDBC, to access the MySQL database, but the same Tomcat Context.xml file continued to be used, without change. Hence Hibernate took advantage of the same JNDI DataSource as the raw JDBC utilized in the first article. This second article, like the first, performed a database query (retrieval) rather than an update, and illustrated the object-oriented nature of a Hibernate database retrieval.

In this third article we have two objectives: First, we want to illustrate a Hibernate database update (actually an insert) which reflects the one-to-many Driver to Car relationship. This update will take place in a properly configured Hibernate transaction. We will illustrate both a commit and a rollback. Second, we will talk about application design in a shopping cart scenario.

Example Servlet that Performs Database Insert

In the earlier articles we looked at Context.xml file, web.xml, HibernateUtil.java, hibernate.cfg.xml, and the mapping files Driver.hbm.xml and Car.hbm.xml and their corresponding Java class files, Driver.java and Car.java. All of these files will continue to be used, without change.

Hence we can immediately focus our attention on the servlet itself, InsertViaHibernate.java. The code follows:

 

The first thing to notice in this program is that - like in the second article - we begin by referencing the provided Hibernate session via the method call SessionFactory.openSession (with the help of HibernateUtil), then we start a Hibernate transaction with: Session.beginTransaction(). The method call to addCarToDriver() causes both object instances - the Car and the Driver - to be saved in the Hibernate Session. No actual database update has yet occurred. When we return to the doGet() method, and it invokes session.getTransaction().commit(), both MySQL tables are actually updated.

In the previous article, we noted the object-oriented approach followed by the Hibernate query. Here you see it again in the updates - only the Java classes are referenced, not the MySQL tables. Nowhere in the Java code is there a reference to the database column car.fk_driver_id, the foreign key in the car table which allows this table and the driver table to be joined together when using raw JDBC.

Note the rollback statement in the catch clause above. This needs to be tested. One way to test it is to change the statement:

 

to:

 

Examine the method getDriverByName(). As written, it assumes the q.list() method will return a java.util.List having (at least) one member. However, this family has no member named "Melvin." As a result, q.list().get(0) will throw an IndexOutOfBoundsException, which is returned to the catch clause in doGet(). In turn, doGet() will invoke getTransaction.rollback().

The Multi-Http-Request Business Transaction

In all the examples thus far in this article series, the method calls

 

and

 

occur in the same Http request. There is no series of screens separating the beginTransaction from the commit, and no intervening user "think time." That is as it should be, because session.beginTransaction opens an actual database transaction, and session.getTransaction.commit actually performs the database commit. These steps should never be separated by any user "think time." A user could conceivably be looking at a given screen, in the middle of a series of screens which in the aggregate comprise a single business transaction, for five minutes or more. To hold the database lock open for such a long time could certainly bottleneck many concurrent transactions from other active users.

The Hibernate reference documentation and other published Hibernate materials refer to this kind of multi-Http-request business transactions as a "long running transaction," or as a "conversational transaction." (This scenario is also referred to as a “business transaction”. Note all three phrases are synonymous, and none of them has anything to do with a database transaction or a Hibernate transaction.) Hibernate provides tools for dealing with long-running transactions. For example, you can detach objects from the Hibernate session before the intervening user "think time," then reattach them when the user submits the next screen and you are ready to work with them again. We have seen recommendations in the Hibernate literature that the scope of the open Hibernate Session be kept short - as limited as possible - as a first choice. We would rather make this recommendation in the strongest possible terms. By all means, open the Hibernate Session and begin the transaction, save whatever object instances you need to save, then commit the transaction, within a single method - as in the examples you have seen to this point in this article series. Be strongly inclined to avoid any other approach. Do not attempt to make use of a Hibernate session which remains open across multiple Http requests.

But what about the multi-Http-request business transaction, you may very reasonably ask? For instance, what about a shopping cart scenario? Let's see.

The Shopping Cart Scenario

How would you design this type of application if there were no such thing as Hibernate, or in a non-Hibernate environment? The approach people have used for years is to use staging tables (or temp tables, or work tables) within the database. Hence as the user marches through the series of screens, with each postback to the server, a new Hibernate Session would be opened, session.beginTransaction would be performed, one or more saves or similar methods would be invoked, then the transaction commit would be performed. Only at the very end of the series of screens - at the end of the business transaction or conversation - would data be transferred from the staging tables to the permanent tables, and the same data be deleted from the staging tables.

This approach, we think, makes as much sense in a Hibernate environment as in a non-Hibernate environment, and keeps everything simple and predictable. And it is quite portable to any database platform.

As a general rule, there can be some small difference between the structure of the permanent table and the structure of the corresponding staging table. The staging table will contain a column such as the Http Session ID to identify the user session. Normally this column would not be present in the permanent table. In your servlet, you can obtain the Http Session ID as follows:

 

The primary key in the two tables, assuming it is a surrogate key assigned by the database or by Hibernate, may perhaps have the same datatype or structure, but will not have the same value. The primary key value in the staging table is nothing but a temporary convenience.

There may be some other small differences between the two record structures, resulting from the fact that the permanent record has been fully processed.

Let's pretend that the car table in the MySQL database needs to go through some kind of multi-Http-request scenario, and needs a staging table. Here is a possible "create table" SQL statement:

 

The corresponding "create table" SQL statement for the permanent car table was presented in the previous article. The only difference between the two SQL statements in this example is the name of the table and the addition of the column http_session_id to the staging table record layout.

Here's the corresponding Java source file, Car_staging.java. It is nearly identical to the module Car.java that was presented in the previous article.

 

And here is the corresponding Hibernate mapping file. Again, it is nearly identical to the mapping file for the permanent car table, shown in the previous article.

 

Of course, you don't have to use staging tables. The alternative exists of detaching and attaching, of deciding just how to employ optimistic locking with Hibernate, and dealing with related issues. But why would this be helpful or desirable? What does it simplify?

Say you wanted to ask the Hibernate Session for all the object instances that had been saved in it during the course of the several interchanges with the user. Presumably you would want an Iterator or an Enumeration. How would you get it? We don't know either. Hence if you may need to do certain late-in-the-process updates or interrogations of the data saved in the Hibernate Session, you will need to save independent references to such data outside of the Hibernate Session, in addition to your having saved this information inside the Hibernate Session.

If you need to store the updated information outside the Hibernate Session, what's the point in also storing it inside the Hibernate Session - from where you cannot even iterate through it? We think the best approach is to store information inside the Hibernate Session when you're ready to commit it.

There are other techniques in widespread use in addition to the staging table technique. For instance, you can store the information from the long-running transaction in the Http session (as opposed to the Hibernate session). However, if a substantial amount of data is stored in the Http session in this way, scalability of your application can be compromised. In addition, server farm clustering can become problematic.

You may also elect to serialize the information in some manner and download it in an HTML Hidden () on the client-side. However, this approach can cause a significant amount of data to be transmitted back-and-forth between the server and client, and you will probably need to do something to make sure the serialized information in the browser is secure. In any web application, the three traditional ways to support the shopping cart scenario are as described above: (1) staging tables (2) attributes in the Http session (3) serialization within HTML Hiddens (probably the least common approach). In non-Hibernate environments, using the most current web MVC technologies - JSF, or Spring MVC, or Struts - you would surely use one of the three approaches described above. We think the same three approaches should be considered in a Hibernate-based web application - make your choice from among them based on the amount of information to be stored in the active shopping cart, and on the other considerations listed above.

 

<!---->
<!---->

<hibernate-mapping></hibernate-mapping>

    <class name="tomcatJndi.Car_staging" table="CAR_STAGING"></class>
        <id name="id" column="CAR_ID" type="”long”"></id>
            <generator class="native"></generator>
        
        <property name="http_session_id"></property>
        <property name="manufacturer"></property>
        <property name="model"></property>
        <property name="year" type="integer"></property>
    


 

package tomcatJndi;

public class Car_staging 
{
    private Long car_id;
    private String http_session_id;
    private String manufacturer;
    private String model;
    private int year;

    public Car() {}

    public Long getId() {
        return car_id;
    }
    private void setId(Long car_id) {	   //Note private visibility
        this.car_id = car_id;
    }
    public String getHttp_session_id() {
        return http_session_id;
    }
    public void setHttp_session_id(String http_session_id) {
        this.http_session_id = http_session_id;
    }
    public String getManufacturer() {
        return manufacturer;
    }
    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
}

 

create table car_staging(
  car_id bigint(20) PRIMARY KEY not null auto_increment,
  http_session_id varchar(64),
  manufacturer varchar(40),
  model varchar(40),
  year int,
  fk_driver_id bigint(20),
  FOREIGN KEY (fk_driver_id) REFERENCES driver (driver_id)
)

 

request.getSession().getId()

 

	    session.getTransaction().commit();	   //Automatically closes session

 

	    session = HibernateUtil.getSessionFactory().openSession();
	    session.beginTransaction();

 

Driver nosuch = getDriverByName("Melvin");

 

Driver jill = getDriverByName("Jill");

 

package tomcatJndi;

import java.io.IOException;
import java.io.PrintWriter;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Query;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

public class InsertViaHibernate extends HttpServlet {

    public InsertViaHibernate() {
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
		
	response.setContentType("text/html");
	PrintWriter out = response.getWriter();
	out.println("<!---->");
	out.println("<html></html>");
	out.println("  <head></head>");
	out.println("  ");

	Session session = null;
		
	try  {
	    session = HibernateUtil.getSessionFactory().openSession();
	    session.beginTransaction();
		
	    Car bigSUV = new Car();
	    bigSUV.setManufacturer("Ford");
	    bigSUV.setModel("Expedition");
	    bigSUV.setYear(2005);
		
	    Driver jill = getDriverByName("Jill", session);
	    addCarToDriver(jill, bigSUV, session);
	    session.getTransaction().commit();	   //Automatically closes session
	}
	catch (Exception e)  {
	    session.getTransaction().rollback();
	    throw new ServletException();
	}
        		
	out.println("  ");
	out.println("");
	out.flush();
	out.close();
    }

    public Driver getDriverByName (String name, Session session)  {
	Query q = session.createQuery("from Driver d where d.name = :name");
	q.setString("name," name);
	Driver newOwner = (Driver) q.list().get(0);
	return newOwner;
    }
		
    public void addCarToDriver(Driver theOwner, Car theCar, Session session)  {		
    	
    	theOwner.getCarsOwned().add(theCar);
    	session.save(theCar);
	session.save(theOwner);
    }

}

 

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();

 

    Session session = HibernateUtil.getSessionFactory().openSession();

 

package tomcatJndi;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.cfg.Configuration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RetrieveViaHibernate extends HttpServlet {

    public RetrieveViaHibernate() {
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!---->");
        out.println("<html></html>");
        out.println("  <head></head>");
        out.println("  ");
        out.print("    This is BRAND NEW ");
        out.print(this.getClass());
        out.println(," using the GET method
"); doQuery(out); //Employ Hibernate to access both Driver and Car tables out.println(" "); out.println(""); out.flush(); out.close(); } private void doQuery(PrintWriter out) { System.out.println("In doQuery"); try { // This step will read hibernate.cfg.xml and prepare hibernate for use Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); String SQL_QUERY ="from Driver driver"; Query query = session.createQuery(SQL_QUERY); for(Iterator it=query.iterate(); it.hasNext(); ) { Driver driver=(Driver)it.next(); out.println("Driver name: " + driver.getName() + ," " ); out.println("Driver age: " + driver.getAge() + "
" ); Set carsOwned = (Set)driver.getCarsOwned(); for (Iterator itOwned = carsOwned.iterator(); itOwned.hasNext(); ) { Car carOwned = (Car)itOwned.next(); out.println("   Manufacturer: " + carOwned.getManufacturer() + ," " ); out.println("Model: " + carOwned.getModel() + ," " ); out.println("Age: " + carOwned.getYear() ); out.println("
"); } } session.getTransaction().commit(); //Automatically closes session } catch(Exception e) { System.out.println(e.getMessage()); } finally{ } } }

 

create table driver(
  driver_id bigint(20) PRIMARY KEY not null,
  name varchar(40),
  age int(11) 
)

 

package tomcatJndi;

import java.util.Set;

public class Driver 
{
    private Long driver_id;

    private String name;
    private int age;
    private Set carsOwned;

    public Driver() {}

    public Long getId() {
        return driver_id;
    }
    private void setId(Long driver_id) {	  //Note private visibility
        this.driver_id = driver_id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }    
    public Set getCarsOwned() {
        return carsOwned;
    }
    public void setCarsOwned(Set carsOwned) {
        this.carsOwned = carsOwned;
    }
}

 

<!---->
<!---->

<hibernate-mapping></hibernate-mapping>

    <class name="tomcatJndi.Driver" table="DRIVER"></class>
        <id name="id" column="DRIVER_ID" type="long"></id>
            <generator class="native"></generator>
        
        <property name="name"></property>
        <property name="age" type="integer"></property>
        
        
            <key column="fk_driver_id"></key>
            <one-to-many class="tomcatJndi.Car"></one-to-many>
        
    


 

create table car(
  car_id bigint(20) PRIMARY KEY not null auto_increment,
  manufacturer varchar(40),
  model varchar(40),
  year int,
  fk_driver_id bigint(20),
  FOREIGN KEY (fk_driver_id) REFERENCES driver (driver_id)
)

 

package tomcatJndi;

public class Car 
{
    private Long car_id;
    private String manufacturer;
    private String model;
    private int year;

    public Car() {}

    public Long getId() {
        return car_id;
    }
    private void setId(Long car_id) {	   //Note private visibility
        this.car_id = car_id;
    }
    public String getManufacturer() {
        return manufacturer;
    }
    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
}

 

<!---->
<!---->

<hibernate-mapping></hibernate-mapping>

    <class name="tomcatJndi.Car" table="CAR"></class>
        <id name="id" column="CAR_ID" type="”long”"></id>
            <generator class="native"></generator>
        
        <property name="manufacturer"></property>
        <property name="model"></property>
        <property name="year" type="integer"></property>
    


 

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/sampdb</property>
<property name="connection.username">root</property>
<property name="connection.password">sammy11</property>

 

<!---->
<!---->

<hibernate-configuration></hibernate-configuration>
 
    <session-factory></session-factory>

        <!---->
	 <property name="connection.datasource">
java:comp/env/jdbc/Sampdb
 </property>
        <property name="show_sql">true</property>
	 <property name="dialect">
org.hibernate.dialect.MySQLDialect
	 </property>
	 <property name="current_session_context_class">thread</property>
	 <property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
 </property>		

        <mapping resource="tomcatJndi/Car.hbm.xml"></mapping>
        <mapping resource="tomcatJndi/Driver.hbm.xml"></mapping>

    


 

	Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
            	
	Car bigSUV = new Car();
	bigSUV.setManufacturer("Ford");
	bigSUV.setModel("Expedition");
	bigSUV.setYear(2005);

	Driver jill = getDriverByName("Jill");
	addCarToDriver(jill, bigSUV);

	session.getTransaction().commit();	   //Automatically closes session

 

       Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
            
           ...[retrieve desired results via a Hibernate query]

       session.getTransaction().commit();	   //Automatically closes session

 

package tomcatJndi;

import org.hibernate.*;
import org.hibernate.cfg.*;

import org.apache.log4j.Logger;

public class HibernateUtil { 

    private static final SessionFactory sessionFactory;

    static {
        try {
	     Logger loggerRoot = Logger.getRootLogger();
	     Logger loggerConnect = Logger.getLogger("Connect");
    		
	     loggerRoot.debug("In HibernateUtil try-clause");
	     loggerRoot.error("In HibernateUtil try-clause");
	     loggerConnect.debug("In HibernateUtil try-clause via loggerConnect DEBUG*****");

            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

 

package tomcatJndi;

import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import org.apache.log4j.Logger;
import org.hibernate.Session;

public class HibernateAppListener implements ServletContextListener	{

    /* Application Startup Event */
    public void	contextInitialized(ServletContextEvent ce) {
Logger loggerConnect = Logger.getLogger("Connect");
		
try  {
    loggerConnect.debug("In HibernateAppListener.contextInitialized");

    Class.forName("tomcatJndi.HibernateUtil").newInstance();			
    loggerConnect.debug("In HibernateAppListener, Class.forName for tomcatJndi.HibernateUtil successful");
}
catch (Exception e)  {			
    loggerConnect.debug("In HibernateAppListener, Class.forName for tomcatJndi.HibernateUtil throws Exception");
	}
    }

    /* Application Shutdown	Event */
    public void	contextDestroyed(ServletContextEvent ce) {}
}

 

<!---->
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"></web-app>
	
  <listener></listener>
    <listener-class></listener-class>tomcatJndi.HibernateAppListener
  
	
  <servlet></servlet>
    <servlet-name></servlet-name>Retrieval
    <servlet-class></servlet-class>tomcatJndi.Retrieval
  
  <servlet></servlet>
    <servlet-name></servlet-name>RetrieveViaHibernate
    <servlet-class></servlet-class>tomcatJndi.RetrieveViaHibernate
  
  <servlet></servlet>
    <servlet-name></servlet-name>InsertViaHibernate
    <servlet-class></servlet-class>tomcatJndi.InsertViaHibernate 
   

  <servlet-mapping></servlet-mapping>
    <servlet-name></servlet-name>Retrieval
    <url-pattern></url-pattern>/Retrieval
  
  <servlet-mapping></servlet-mapping>
    <servlet-name></servlet-name>RetrieveViaHibernate
    <url-pattern></url-pattern>/RetrieveViaHibernate
  
  <servlet-mapping></servlet-mapping>
    <servlet-name></servlet-name>InsertViaHibernate
    <url-pattern></url-pattern>/InsertViaHibernate
  


 

package TomcatJNDI;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Retrieval extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Retrieval() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!---->");
		out.println("<html></html>");
		out.println("  <head></head>");
		out.println("  ");

		try  {
			InitialContext initCtx = new InitialContext();
			DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/Sampdb");
			Connection conn = ds.getConnection();

			out.println("

"); out.println("Connection from DataSource successfully opened!
"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from car"); while (rs.next() ) { String onerow = "Car manufacturer: " + rs.getString("manufacturer") + ," model: " + rs.getString("model") + ," year: " + rs.getString("year") + "
"; out.println(onerow); } rs.close(); stmt.close(); conn.close(); initCtx.close(); out.println("

"); out.println("Connection from DataSource successfully closed!
"); } catch(Exception e) { out.println("

"); out.println("Connection from DataSource NOT successfully opened!
"); out.println(e.getMessage() + "
"); } out.println(" "); out.println(""); out.flush(); out.close(); } public void init() throws ServletException { // Put your code here } } //End class Retrieval

 

InitialContext initCtx = new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/Sampdb");
Connection conn = ds.getConnection();

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from car");

 

<!---->

<context path="/BasicWeb" reloadable="true" workdir="C:\Tomcat 5.5\webapps\BasicWeb" docbase="C:\Tomcat 5.5\webapps\BasicWeb"></context>

  <resource name="jdbc/Sampdb" password="sam123" type="javax.sql.DataSource" username="sam" url="jdbc:mysql://localhost:3306/sampdb?autoReconnect=true" auth="Container" driverclassname="com.mysql.jdbc.Driver" maxwait="10000" maxidle="30" maxactive="100"></resource>


分享到:
评论

相关推荐

    hibernate 3.1+tomcat 5.5.x(配置jndi)

    ### hibernate 3.1+tomcat 5.5.x(配置jndi) #### 一、引言 随着Tomcat从5.0版本升级到5.5.x版本,其配置发生了诸多变化,特别是对Tomcat JNDI的配置方式产生了显著的影响。这对希望在Hibernate安装中利用Tomcat ...

    Hibernate 3.1+Tomcat 5.5.X(配置JNDI篇)

    【标题】:“Hibernate 3.1+Tomcat 5.5.X(配置JNDI篇)” 【描述】:文章介绍了在Tomcat 5.5.X版本中如何配置Hibernate 3.1,特别是关于JNDI(Java Naming and Directory Interface)的设置方法。 【标签】:...

    tomcat5.5.X域名转向和连接池配置的server.xml文件

    在Tomcat 5.5.x版本中,`server.xml`是服务器的主要配置文件,它包含了关于服务器设置、连接器、容器以及其他关键组件的配置信息。本篇文章将详细解释如何在`server.xml`中配置域名转向和连接池。 ### 域名转向...

    Tomcat5.5.X下配置连接池

    Tomcat 5.5.x版本虽然相对较老,但依然有很多项目在运行,因此了解如何在该版本下配置连接池是至关重要的。连接池是一种管理数据库连接的技术,它能够有效地复用数据库连接,减少创建和销毁连接的开销,提高系统性能...

    marshalsec-0.0.3-SNAPSHOT-all.jar

    java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.RMIRefServer http://VPS/ExportObject 1099 开启ldap服务 java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer ...

    JMX以RMI方式连接的场景示例

    JMX以RMI方式连接的场景示例 JMX(Java Management Extensions)是一种Java技术,用于管理和监控应用程序。JMX框架主要由三部分组成:MBean(Managed Bean)、Agent(代理)和Client(客户端)。...

    tomcat5.5.rar )

    Tomcat 5.5是一款经典的Java Servlet和JavaServer Pages(JSP)容器,由Apache软件基金会开发并维护。它是开源的、免费的Web应用服务器,主要用于运行基于Java技术的Web应用程序。Tomcat作为轻量级应用服务器,在中...

    hibernate-configuration-3.0.dtd、hibernate-mapping-3.0.dtd

    《深入理解Hibernate配置与映射:hibernate-configuration-3.0.dtd与hibernate-mapping-3.0.dtd解析》 在Java世界里,Hibernate作为一款强大的对象关系映射(ORM)框架,极大地简化了数据库操作。而`hibernate-...

    com.sun.jndi.ldap.jar

    《com.sun.jndi.ldap.jar:Maven中的 LDAP 相关库解析》 在Java开发中,`com.sun.jndi.ldap.jar`是一个重要的库文件,它包含了Java Naming and Directory Interface (JNDI) 的 LDAP(Lightweight Directory Access ...

    JBOSS使用指南

    2.1因为客户端需要通过JNDI查找EJB,那么JNDI 是什么 2.2 Stateless Session Beans(无状态bean)开发 2.3 Stateless Session Bean与Stateful Session Bean的区三别 2.4 Session Bean的生命周期 3.消息驱动Bean ...

    hibernate.properties

    hibernate.connection.url jdbc:hsqldb:./build/db/hsqldb/hibernate #hibernate.connection.url jdbc:hsqldb:hsql://localhost #hibernate.connection.url jdbc:hsqldb:test ## H2 (www.h2database.com) #...

    Tomcat5.5 的dbcp配置

    本文将详细讲解如何在Tomcat 5.5中配置DBCP,以及JNDI(Java Naming and Directory Interface)的设置方法。 首先,了解DBCP的作用。数据库连接池允许应用复用已存在的数据库连接,而不是每次请求时都创建新的连接...

    Tomcat7.x 64位系统

    6. **JNDI**:Java Naming and Directory Interface,Tomcat7.x支持JNDI,允许查找和绑定资源,如数据源。 7. **热部署**:当`webapps`目录下的WAR文件或目录被修改时,Tomcat可以自动检测并重新部署应用。 8. **...

    apache-tomcat-8.5.97-windows-x.zip

    这个压缩包文件 "apache-tomcat-8.5.97-windows-x.zip" 包含了Apache Tomcat 8.5.97 版本在Windows操作系统上的安装内容。下面将详细介绍Tomcat的相关知识点。 1. **Apache Tomcat 的角色**: - Tomcat 是一个实现...

    apache-tomcat-9.0.41.rar

    Apache Tomcat 9.0.41 是一个广泛使用的开源软件,它是一个符合Java Servlet和JavaServer Pages(JSP)规范的应用服务器,主要用于部署和运行Java Web应用程序。这个版本的Tomcat无需安装过程,用户只需解压文件后...

    windows安装版-tomcat8.5.57.zip

    1. **版本特性**:Tomcat 8.5.x系列是基于Java EE 7标准的,相比于之前的版本,它增加了对WebSocket、JSON-P、JAX-RS 2.0等新特性的支持。 2. **配置**:在Windows环境下,可以通过修改“conf/server.xml”文件来...

Global site tag (gtag.js) - Google Analytics