Archive for September, 2007

Transaction essentials You don t (Web host sites) want to use this

Monday, September 17th, 2007

Transaction essentials You don t want to use this example as a template in your own application, because you should hide the exception handling with generic infrastructure code. You can, for example, write a single error handler for RuntimeException that knows when and how to roll back a transaction. The same can be said about opening and closing a Session. We discuss this with more realistic examples later in the next chapter and again in chapter 16, section 16.1.3, The Open Session in View pattern. Hibernate throws typed exceptions, all subtypes of RuntimeException that help you identify errors: The most common HibernateException is a generic error. You have to either check the exception message or find out more about the cause by calling getCause() on the exception. A JDBCException is any exception thrown by Hibernate s internal JDBC layer. This kind of exception is always caused by a particular SQL statement, and you can get the offending statement with getSQL(). The internal exception thrown by the JDBC connection (the JDBC driver, actually) is available with getSQLException() or getCause(), and the database- and vendor-specific error code is available with getErrorCode(). Hibernate includes subtypes of JDBCException and an internal converter that tries to translate the vendor-specific error code thrown by the database driver into something more meaningful. The built-in converter can produce JDBCConnectionException, SQLGrammarException, LockAquisition- Exception, DataException, and ConstraintViolationException for the most important database dialects supported by Hibernate. You can either manipulate or enhance the dialect for your database, or plug in a SQLExceptionConverterFactory to customize this conversion. Other RuntimeExceptions thrown by Hibernate should also abort a transaction. You should always make sure you catch RuntimeException, no matter what you plan to do with any fine-grained exception-handling strategy. You now know what exceptions you should catch and when to expect them. However, one question is probably on your mind: What should you do after you ve caught an exception? All exceptions thrown by Hibernate are fatal. This means you have to roll back the database transaction and close the current Session. You aren t allowed to continue working with a Session that threw an exception.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Web hosting asp - CHAPTER 10 Transactions and concurrency exceptions thrown by

Sunday, September 16th, 2007

CHAPTER 10 Transactions and concurrency exceptions thrown by Hibernate are fatal. In many cases, the best a developer can do in this situation is to clean up, display an error message, and exit the application. Therefore, starting with Hibernate 3.x, all exceptions thrown by Hibernate are subtypes of the unchecked Runtime- Exception, which is usually handled in a single location in an application. This also makes any Hibernate template or wrapper API obsolete. First, even though we admit that you wouldn t write your application code with dozens (or hundreds) of try/catch blocks, the example we showed isn t complete. This is an example of the standard idiom for a Hibernate unit of work with a database transaction that contains real exception handling: Session session = null; Transaction tx = null; try { session = sessionFactory.openSession(); tx = session.beginTransaction(); tx.setTimeout(5); concludeAuction(session); tx.commit(); } catch (RuntimeException ex) { try { tx.rollback(); } catch (RuntimeException rbEx) { log.error(”Couldn t roll back transaction”, rbEx); } throw ex; } finally { session.close(); } Any Hibernate operation, including flushing the persistence context, can throw a RuntimeException. Even rolling back a transaction can throw an exception! You want to catch this exception and log it; otherwise, the original exception that led to the rollback is swallowed. An optional method call in the example is setTimeout(), which takes the number of seconds a transaction is allowed to run. However, real monitored transactions aren t available in a Java SE environment. The best Hibernate can do if you run this code outside of an application server (that is, without a transaction manager) is to set the number of seconds the driver will wait for a Prepared- Statement to execute (Hibernate exclusively uses prepared statements). If the limit is exceeded, an SQLException is thrown.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Transaction essentials Session is flushed. This happens when (Web server version)

Saturday, September 15th, 2007

Transaction essentials Session is flushed. This happens when you call commit() on the Transaction, by default. After you commit the transaction (or roll it back), the database connection is released and unbound from the Session. Beginning a new transaction with the same Session obtains another connection from the pool. Closing the Session releases all other resources (for example, the persistence context); all managed persistent instances are now considered detached. FAQ Is it faster to roll back read-only transactions? If code in a transaction reads data but doesn t modify it, should you roll back the transaction instead of committing it? Would this be faster? Apparently, some developers found this to be faster in some special circumstances, and this belief has spread through the community. We tested this with the more popular database systems and found no difference. We also failed to discover any source of real numbers showing a performance difference. There is also no reason why a database system should have a suboptimal implementation why it should not use the fastest transaction cleanup algorithm internally. Always commit your transaction and roll back if the commit fails. Having said that, the SQL standard includes a SET TRANSACTION READ ONLY statement. Hibernate doesn t support an API that enables this setting, although you could implement your own custom Transaction and TransactionFactory to add this operation. We recommend you first investigate if this is supported by your database and what the possible performance benefits, if any, will be. We need to discuss exception handling at this point. Handling exceptions If concludeAuction() as shown in the last example (or flushing of the persistence context during commit) throws an exception, you must force the transaction to roll back by calling tx.rollback(). This rolls back the transaction immediately, so no SQL operation you sent to the database has any permanent effect. This seems straightforward, although you can probably already see that catching RuntimeException whenever you want to access the database won t result in nice code. NOTE A history of exceptions Exceptions and how they should be handled always end in heated debates between Java developers. It isn t surprising that Hibernate has some noteworthy history as well. Until Hibernate 3.x, all exceptions thrown by Hibernate were checked exceptions, so every Hibernate API forced the developer to catch and handle exceptions. This strategy was influenced by JDBC, which also throws only checked exceptions. However, it soon became clear that this doesn t make sense, because all
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

CHAPTER 10 Transactions (My web server) and concurrency Programmatic transactions in

Saturday, September 15th, 2007

CHAPTER 10 Transactions and concurrency Programmatic transactions in Java SE You configure Hibernate to create a JDBC connection pool for you, as you did in The database connection pool in chapter 2, section 2.1.3. In addition to the connection pool, no additional configuration settings are necessary if you re writing a Java SE Hibernate application with the Transaction API: The hibernate.transaction.factory_classoption defaults to org.hibernate.transaction.JDBCTransactionFactory, which is the correct factory for the Transaction API in Java SE and for direct JDBC. You can extend and customize the Transaction interface with your own implementation of a TransactionFactory. This is rarely necessary but has some interesting use cases. For example, if you have to write an audit log whenever a transaction is started, you can add this logging to a custom Transaction implementation. Hibernate obtains a JDBC connection for each Session you re going to work with: Session session = null; Transaction tx = null; try { session = sessionFactory.openSession(); tx = session.beginTransaction(); concludeAuction(session); tx.commit(); } catch (RuntimeException ex) { tx.rollback(); } finally { session.close(); } A Hibernate Session is lazy. This is a good thing it means it doesn t consume any resources unless they re absolutely needed. A JDBC Connection from the connection pool is obtained only when the database transaction begins. The call to beginTransaction() translates into setAutoCommit(false) on the fresh JDBC Connection. The Session is now bound to this database connection, and all SQL statements (in this case, all SQL required to conclude the auction) are sent on this connection. All database statements execute inside the same database transaction. (We assume that the concludeAuction() method calls the given Session to access the database.) We already talked about write-behind behavior, so you know that the bulk of SQL statements are executed as late as possible, when the persistence context of the
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Transaction essentials org.hibernate.Transaction Unified (Web hosting domain) transaction demarcation in Hibernate

Friday, September 14th, 2007

Transaction essentials org.hibernate.Transaction Unified transaction demarcation in Hibernate applications. It works in a nonmanaged plain JDBC environment and also in an application server with JTA as the underlying system transaction service. The main benefit, however, is tight integration with persistence context management for example, a Session is flushed automatically when you commit. A persistence context can also have the scope of this transaction (useful for conversations; see the next chapter). Use this API in Java SE if you can t have a JTA-compatible transaction service. javax.transaction.UserTransaction Standardized interface for programmatic transaction control in Java; part of JTA. This should be your primary choice whenever you have a JTA-compatible transaction service and want to control transactions programmatically. javax.persistence.EntityTransaction Standardized interface for programmatic transaction control in Java SE applications that use Java Persistence. Declarative transaction demarcation, on the other hand, doesn t require extra coding; and by definition, it solves the problem of portability. Declarative transaction demarcation In your application, you declare (for example, with annotations on methods) when you wish to work inside a transaction. It s then the responsibility of the application deployer and the runtime environment to handle this concern. The standard container that provides declarative transaction services in Java is an EJB container, and the service is also called container-managed transactions (CMT). We ll again write EJB session beans to show how both Hibernate and Java Persistence can benefit from this service. Before you decide on a particular API, or for declarative transaction demarcation, let s explore these options step by step. First, we assume you re going to use native Hibernate in a plain Java SE application (a client/server web application, desktop application, or any two-tier system). After that, you ll refactor the code to run in a managed Java EE environment (and see how to avoid that refactoring in the first place). We also discuss Java Persistence along the way. 10.1.2 Transactions in a Hibernate application Imagine that you re writing a Hibernate application that has to run in plain Java; no container and no managed database resources are available.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Programmatic transaction (Web hosting billing) demarcation In a nonmanaged environment, the

Thursday, September 13th, 2007

Programmatic transaction demarcation In a nonmanaged environment, the JDBC API is used to mark transaction boundaries. You begin a transaction by calling setAutoCommit(false) on a JDBC Connection and end it by calling commit(). You may, at any time, force an immediate rollback by calling rollback(). In a system that manipulates data in several databases, a particular unit of work involves access to more than one resource. In this case, you can t achieve atomicity with JDBC alone. You need a transaction manager that can handle several resources in one system transaction. Such transaction-processing systems expose the Java Transaction API (JTA) for interaction with the developer. The main API in JTA is the UserTransaction interface with methods to begin() and commit() a system transaction. Furthermore, programmatic transaction management in a Hibernate application is exposed to the application developer via the Hibernate Transaction interface. You aren t forced to use this API Hibernate also lets you begin and end JDBC transactions directly, but this usage is discouraged because it binds your code to direct JDBC. In a Java EE environment (or if you installed it along with your Java SE application), a JTA-compatible transaction manager is available, so you should call the JTA UserTransaction interface to begin and end a transaction programmatically. However, the Hibernate Transaction interface, as you may have guessed, also works on top of JTA. We ll show you all these options and discuss portability concerns in more detail. Programmatic transaction demarcation with Java Persistence also has to work inside and outside of a Java EE application server. Outside of an application server, with plain Java SE, you re dealing with resource-local transactions; this is what the EntityTransaction interface is good for you ve seen it in previous chapters. Inside an application server, you call the JTA UserTransaction interface to begin and end a transaction. Let s summarize these interfaces and when they re used: java.sql.Connection Plain JDBC transaction demarcation with set- AutoCommit(false), commit(), and rollback(). It can but shouldn t be used in a Hibernate application, because it binds your application to a plain JDBC environment.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Managed web hosting - CHAPTER 16 Creating and testing layered applications this

Thursday, September 13th, 2007

CHAPTER 16 Creating and testing layered applications this point. You can not load uninitialized proxies and collections of an entity instance that is in detached state. If the Hibernate Session and therefore the persistence context is always closed at the end of the action s execute() method, Hibernate throws a LazyInitializationException when this unloaded association (or collection) is accessed. The persistence context is no longer available, so Hibernate can t load the lazy collection on access. FAQ Why can t Hibernate open a new Session if it has to lazy load objects? The Hibernate Session is the persistence context, the scope of object identity. Hibernate guarantees that there is at most one in-memory representation of a particular database row, in one persistence context. Opening a Session on-demand, behind the scenes, would also create a new persistence context, and all objects loaded in this identity scope would potentially conflict with objects loaded in the original persistence context. You can t load data on-demand when an object is out of the guaranteed scope of object identity when it s detached. On the other hand, you can load data as long as the objects are in persistent state, managed by a Session, even when the original transaction has been committed. In such a scenario, you have to enable the autocommit mode, as discussed in chapter 10, section 10.3, Nontransactional data access. We recommend that you don t use the autocommit mode in a web application; it s much easier to extend the original Session and transaction to span the whole request. In systems where you can t easily begin and end a transaction when objects have to be loaded on-demand inside a Session, such as Swing desktop applications that use Hibernate, the autocommit mode is useful. A first solution would be to ensure that all needed associations and collections are fully initialized before forwarding to the view (we discuss this later), but a more convenient approach in a two-tiered architecture with a colocated presentation and persistence layer is to leave the persistence context open until the view is completely rendered. The OSIV pattern allows you to have a single Hibernate persistence context per request, spanning the rendering of the view and potentially multiple action execute()s. It can also be implemented easily for example, with a servlet filter: public class HibernateSessionRequestFilter implements Filter { private SessionFactory sf; private static Log log = …; public void doFilter(ServletRequest request,
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

21 Storing Feed (Web design) Data In this chapter we

Thursday, September 13th, 2007

21 Storing Feed Data In this chapter we extend and develop the ideas and techniques discussed regarding the modeling of feeds with an emphasis on building practical systems. In this chapter, you learn about: . Simple DOM-based XML storage . Flat relational database storage . RDF-modeled data storage There is another popular set of techniques for storing feed data based around native XML databases and the XPath standard for addressing their contents. These techniques tend to depend on the implementation details of the database being used, so coverage will be left to the practical example in Chapter 26. The Document Object Model In the previous chapter, we treated the modeling of a feed as a document rather dismissively. But the drawbacks we described didn t have anything to do with document modeling, but with treating the feed as an integral, fixed document. The Document Object Model (DOM) is a standard interface for working with documents that allows them to be created from or broken down into component parts. The DOM specifications can be found at www.w3.org/DOM/. DOM treats documents as tree structures comprising a root, branch, and leaf elements. DOM treats elements, attributes, and text as elements in a parent-child structure, and in a typical XML document the hierarchy is fairly clear, as you can see in Figure 21-1.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Tech Idol Shelley http://example.org/idol/shelley Shelley first impressed the

Thursday, September 13th, 2007


http://example.org/idol/shelley Shelley first impressed the judges with her 3-line backup script, then trampled the opposition with her cover of These Boots Were Made for Walking .
Hint: The W3C s validator provides graphic views at www.w3.org/RDF/Validator/. 283 Modeling Feed Data
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Exercises 1. The following is an Atom 0.3 (Graphic web design)

Wednesday, September 12th, 2007

Exercises 1. The following is an Atom 0.3 feed approximating the RSS 2.0 example: The hottest performers on the Net!
http://example.org/idol/shelley
2004-07-01T09:39:21Z Shelley first impressed the judges with her 3-line backup script, then trampled the opposition with her cover of These Boots Were Made for Walking .
http://example.org/idol/sam
2004-07-01T 08:37:3Z Test-driven development while plate-spinning? Sam s the man.
http://example.org/idol/marc
2004-07-01T08:56:02Z Marc s multimedia presentation of O Sole Mio served him well, but perhaps he should have kept those knees covered!
Modify the CSS rss.css to style this document in a similar fashion to the RSS document. 2. While discussing the RDF model, a simple node and arc diagram of a single item was shown. Later you saw a larger diagram of blogroll data. Of course it s possible to represent the data in a full RSS 1.0 feed in this fashion. Here is part of the sample feed data rewritten in RSS 1.0. See if you can sketch out the node and arc diagram for this data: 282 Chapter 20
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.