Archive for September, 2007

Transaction essentials utx.rollback(); } catch (RuntimeException rbEx) {

Sunday, September 30th, 2007

Transaction essentials utx.rollback(); } catch (RuntimeException rbEx) { log.error(”Couldn’t roll back transaction”, rbEx); } throw ex; } finally { em.close(); } The persistence context of the EntityManager is scoped to the JTA transaction. All SQL statements flushed by this EntityManager are executed inside the JTA transaction on a database connection that is enlisted with the transaction. The persistence context is flushed and closed automatically when the JTA transaction commits. You could use several EntityManagers to access several databases in the same system transaction, just as you d use several Sessions in a native Hibernate application. Note that the scope of the persistence context changed! It s now scoped to the JTA transaction, and any object that was in persistent state during the transaction is considered detached once the transaction is committed. The rules for exception handling are equivalent to those for resource-local transactions. If you use JTA in EJBs, don t forget to set @TransactionManagement(TransactionManagementType.BEAN) on the class to enable BMT. You won t often use Java Persistence with JTA and not also have an EJB container available. If you don t deploy a stand-alone JTA implementation, a Java EE 5.0 application server will provide both. Instead of programmatic transaction demarcation, you ll probably utilize the declarative features of EJBs. Java Persistence and CMT Let s refactor the ManageAuction EJB session bean from the earlier Hibernate- only examples to Java Persistence interfaces. You also let the container inject an EntityManager: @Stateless public class ManageAuctionBean implements ManageAuction { @PersistenceContext(unitName = “auctionDB”) private EntityManager auctionEM; @PersistenceContext(unitName = “billingDB”) private EntityManager billingEM; @TransactionAttribute(TransactionAttributeType.REQUIRED) public void endAuction(Item item) throws AuctionNotValidException { concludeAuction(auctionEM, item);
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Web hosting domains - CHAPTER 10 Transactions and concurrency Exceptions thrown by

Sunday, September 30th, 2007

CHAPTER 10 Transactions and concurrency Exceptions thrown by JPA are subtypes of RuntimeException. Any exception invalidates the current persistence context, and you aren t allowed to continue working with the EntityManager once an exception has been thrown. Therefore, all the strategies we discussed for Hibernate exception handling also apply to Java Persistence exception handling. In addition, the following rules apply: Any exception thrown by any method of the EntityManager interfaces triggers an automatic rollback of the current transaction. Any exception thrown by any method of the javax.persistence.Query interface triggers an automatic rollback of the current transaction, except for NoResultException and NonUniqueResultException. So, the previous code example that catches all exceptions also executes a rollback for these exceptions. Note that JPA doesn t offer fine-grained SQL exception types. The most common exception is javax.persistence.PersistenceException. All other exceptions thrown are subtypes of PersistenceException, and you should consider them all fatal except NoResultException and NonUniqueResultException. However, you may call getCause() on any exception thrown by JPA and find a wrapped native Hibernate exception, including the fine-grained SQL exception types. If you use Java Persistence inside an application server or in an environment that at least provides JTA (see our earlier discussions for Hibernate), you call the JTA interfaces for programmatic transaction demarcation. The EntityTransaction interface is available only for resource-local transactions. JTA transactions with Java Persistence If your Java Persistence code is deployed in an environment where JTA is available, and you want to use JTA system transactions, you need to call the JTA UserTransaction interface to control transaction boundaries programmatically: UserTransaction utx = (UserTransaction) new InitialContext() .lookup(”java:comp/UserTransaction”); EntityManager em = null; try { utx.begin(); em = emf.createEntityManager(); concludeAuction(em); utx.commit(); } catch (RuntimeException ex) { try {
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Web site management - Transaction essentials 10.1.3 Transactions with Java Persistence With

Saturday, September 29th, 2007

Transaction essentials 10.1.3 Transactions with Java Persistence With Java Persistence, you also have the design choice to make between programmatic transaction demarcation in application code or declarative transaction demarcation handled automatically by the runtime container. Let s investigate the first option with plain Java SE and then repeat the examples with JTA and EJB components. The description resource-local transaction applies to all transactions that are controlled by the application (programmatic) and that aren t participating in a global system transaction. They translate directly into the native transaction system of the resource you re dealing with. Because you re working with JDBC databases, this means a resource-local transaction translates into a JDBC database transaction. Resource-local transactions in JPA are controlled with the EntityTransaction API. This interface exists not for portability reasons, but to enable particular features of Java Persistence for instance, flushing of the underlying persistence context when you commit a transaction. You ve seen the standard idiom of Java Persistence in Java SE many times. Here it is again with exception handling: EntityManager em = null; EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); concludeAuction(em); tx.commit(); } catch (RuntimeException ex) { try { tx.rollback(); } catch (RuntimeException rbEx) { log.error(”Couldn’t roll back transaction”, rbEx); } throw ex; } finally { em.close(); } This pattern is close to its Hibernate equivalent, with the same implications: You have to manually begin and end a database transaction, and you must guarantee that the application-managed EntityManager is closed in a finally block. (Although we often show code examples that don t handle exceptions or are wrapped in a try/catch block, this isn t optional.)
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

CHAPTER 10 Transactions and (Free web servers) concurrency public void endAuction(Item

Friday, September 28th, 2007

CHAPTER 10 Transactions and concurrency public void endAuction(Item item) { Session session1 = auctionDatabase.openSession(); Session session2 = billingDatabase.openSession(); concludeAuction(session1, item); billAuction(session2, item); } … } The container notices your declaration of a TransactionAttribute and applies it to the endAuction() method. If no system transaction is running when the method is called, a new transaction is started (it s REQUIRED). Once the method returns, and if the transaction was started when the method was called (and not by anyone else), the transaction commits. The system transaction is automatically rolled back if the code inside the method throws a RuntimeException. We again show two SessionFactorys for two databases, for the sake of the example. They could be assigned with a JNDI lookup (Hibernate can bind them there at startup) or from an enhanced version of HibernateUtil. Both obtain database connections that are enlisted with the same container-managed transaction. And, if the container s transaction system and the resources support it, you again get a two-phase commit protocol that ensures atomicity of the transaction across databases. You have to set some configuration options to enable CMT with Hibernate: The hibernate.transaction.factory_class option must be set to org. hibernate.transaction.CMTTransactionFactory. You need to set hibernate.transaction.manager_lookup_class to the right lookup class for your application server. Also note that all EJB session beans default to CMT, so if you want to disable CMT and call the JTA UserTransaction directly in any session bean method, annotate the EJB class with @TransactionManagement(TransactionManagementType. BEAN). You re then working with bean-managed transactions (BMT). Even if it may work in most application servers, mixing CMT and BMT in a single bean isn t allowed by the Java EE specification. The CMT code already looks much nicer than the programmatic transaction demarcation. If you configure Hibernate to use CMT, it knows that it should flush and close a Session that participates in a system transaction automatically. Furthermore, you ll soon improve this code and even remove the two lines that open a Hibernate Session. Let s look at transaction handling in a Java Persistence application.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Transaction essentials try { utx.rollback(); } catch (RuntimeException

Thursday, September 27th, 2007

Transaction essentials try { utx.rollback(); } catch (RuntimeException rbEx) { log.error(”Couldn’t roll back transaction”, rbEx); } throw ex; } The session1 and session2 persistence context is now flushed automatically during commit of the UserTransaction, and both are closed after the transaction completes. Our advice is to use JTA directly whenever possible. You should always try to move the responsibility for portability outside of the application and, if you can, require deployment in an environment that provides JTA. Programmatic transaction demarcation requires application code written against a transaction demarcation interface. A much nicer approach that avoids any nonportable code spread throughout your application is declarative transaction demarcation. Container-managed transactions Declarative transaction demarcation implies that a container takes care of this concern for you. You declare if and how you want your code to participate in a transaction. The responsibility to provide a container that supports declarative transaction demarcation is again where it belongs, with the application deployer. CMT is a standard feature of Java EE and, in particular, EJB. The code we ll show you next is based on EJB 3.0 session beans ( Java EE only); you define transaction boundaries with annotations. Note that the actual data-access code doesn t change if you have to use the older EJB 2.1 session beans; however, you have to write an EJB deployment descriptor in XML to create your transaction assembly this is optional in EJB 3.0. (A stand-alone JTA implementation doesn t provide container-managed and declarative transactions. However, JBoss Application Server is available as a modular server with a minimal footprint, and it can provide only JTA and an EJB 3.0 container, if needed.) Suppose that an EJB 3.0 session bean implements an action that ends an auction. The code you previously wrote with programmatic JTA transaction demarcation is moved into a stateless session bean: @Stateless public class ManageAuctionBean implements ManageAuction { @TransactionAttribute(TransactionAttributeType.REQUIRED)
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

CHAPTER 10 Transactions and concurrency utx.rollback(); } catch

Wednesday, September 26th, 2007

CHAPTER 10 Transactions and concurrency utx.rollback(); } catch (RuntimeException rbEx) { log.error(”Couldn’t roll back transaction”, rbEx); } throw ex; } finally { session1.close(); session2.close(); } (Note that this code snippet can throw some other, checked exceptions, like a NamingException from the JNDI lookup. You need to handle these accordingly.) First, a handle on a JTA UserTransaction must be obtained from the JNDI registry. Then, you begin and end a transaction, and the (container-provided) database connections used by all Hibernate Sessions are enlisted in that transaction automatically. Even if you aren t using the Transaction API, you should still configure hibernate.transaction.factory_class and hibernate.transaction. manager_lookup_class for JTA and your environment, so that Hibernate can interact with the transaction system internally. With default settings, it s also your responsibility to flush() each Session manually to synchronize it with the database (to execute all SQL DML). The Hibernate Transaction API did this automatically for you. You also have to close all Sessions manually. On the other hand, you can enable the hibernate.transaction.flush_before_completion and/or the hibernate.transaction.auto_ close_session configuration options and let Hibernate take care of this for you again flushing and closing is then part of the internal synchronization procedure of the transaction manager and occurs before (and after, respectively) the JTA transaction ends. With these two settings enabled the code can be simplified to the following: UserTransaction utx = (UserTransaction) new InitialContext() .lookup(”java:comp/UserTransaction”); Session session1 = null; Session session2 = null; try { utx.begin(); session1 = auctionDatabase.openSession(); session2 = billingDatabase.openSession(); concludeAuction(session1); billAuction(session2); utx.commit(); } catch (RuntimeException ex) {
Check Tomcat Web Hosting services for best quality webspace to host your web application.

How to cite a web site - Transaction essentials } finally { session.close(); } However,

Tuesday, September 25th, 2007

Transaction essentials } finally { session.close(); } However, the database connection-handling is slightly different. Hibernate obtains a managed database connection for each Session you re using and, again, tries to be as lazy as possible. Without JTA, Hibernate would hold on to a particular database connection from the beginning until the end of the transaction. With a JTA configuration, Hibernate is even more aggressive: A connection is obtained and used for only a single SQL statement and then is immediately returned to the managed connection pool. The application server guarantees that it will hand out the same connection during the same transaction, when it s needed again for another SQL statement. This aggressive connection-release mode is Hibernate s internal behavior, and it doesn t make any difference for your application and how you write code. (Hence, the code example is line-by-line the same as the last one.) A JTA system supports global transaction timeouts; it can monitor transactions. So, setTimeout() now controls the global JTA timeout setting equivalent to calling UserTransaction.setTransactionTimeout(). The Hibernate Transaction API guarantees portability with a simple change of Hibernate configuration. If you want to move this responsibility to the application deployer, you should write your code against the standardized JTA interface, instead. To make the following example a little more interesting, you ll also work with two databases (two SessionFactorys) inside the same system transaction: UserTransaction utx = (UserTransaction) new InitialContext() .lookup(”java:comp/UserTransaction”); Session session1 = null; Session session2 = null; try { utx.begin(); session1 = auctionDatabase.openSession(); session2 = billingDatabase.openSession(); concludeAuction(session1); billAuction(session2); session1.flush(); session2.flush(); utx.commit(); } catch (RuntimeException ex) { try {
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

CHAPTER 10 Transactions and concurrency examples, which all (Web hosting mysql)

Thursday, September 20th, 2007

CHAPTER 10 Transactions and concurrency examples, which all call the Hibernate Transaction API, on a Java EE application server, you need to switch the Hibernate configuration to JTA: The hibernate.transaction.factory_class option must be set to org. hibernate.transaction.JTATransactionFactory. Hibernate needs to know the JTA implementation on which you re deploying, for two reasons: First, different implementations may expose the JTA UserTransaction, which Hibernate has to call internally now, under different names. Second, Hibernate has to hook into the synchronization process of the JTA transaction manager to handle its caches. You have to set the hibernate.transaction.manager_lookup_class option to configure both: for example, to org.hibernate.transaction.JBossTransaction- ManagerLookup. Lookup classes for the most common JTA implementations and application servers are packaged with Hibernate (and can be customized if needed). Check the Javadoc for the package. Hibernate is no longer responsible for managing a JDBC connection pool; it obtains managed database connections from the runtime container. These connections are exposed by the JTA provider through JNDI, a global registry. You must configure Hibernate with the right name for your database resources on JNDI, as you did in chapter 2, section 2.4.1, Integration with JTA. Now the same piece of code you wrote earlier for Java SE directly on top of JDBC will work in a JTA environment with managed datasources: 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;
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Transaction essentials isn t the only way to get (Cool web site)

Wednesday, September 19th, 2007

Transaction essentials isn t the only way to get them. You can obtain a stand-alone JTA provider if managed resources are all you need. Open source stand-alone JTA providers include JBoss Transactions (http://www.jboss.com/products/transactions), ObjectWeb JOTM (http://jotm.objectweb.org), and others. You can install such a JTA service along with your Hibernate application (in Tomcat, for example). It will manage a pool of database connections for you, provide JTA interfaces for transaction demarcation, and provide managed database connections through a JNDI registry. The following are benefits of managed resources with JTA and reasons to use this Java EE service: A transaction-management service can unify all resources, no matter of what type, and expose transaction control to you with a single standardized API. This means that you can replace the Hibernate Transaction API and use JTA directly everywhere. It s then the responsibility of the application deployer to install the application on (or with) a JTA-compatible runtime environment. This strategy moves portability concerns where they belong; the application relies on standardized Java EE interfaces, and the runtime environment has to provide an implementation. A Java EE transaction manager can enlist multiple resources in a single transaction. If you work with several databases (or more than one resource), you probably want a two-phase commit protocol to guarantee atomicity of a transaction across resource boundaries. In such a scenario, Hibernate is configured with several SessionFactorys, one for each database, and their Sessions obtain managed database connections that all participate in the same system transaction. The quality of JTA implementations is usually higher compared to simple JDBC connection pools. Application servers and stand-alone JTA providers that are modules of application servers usually have had more testing in high-end systems with a large transaction volume. JTA providers don t add unnecessary overhead at runtime (a common misconception). The simple case (a single JDBC database) is handled as efficiently as with plain JDBC transactions. The connection pool managed behind a JTA service is probably much better software than a random connection pooling library you d use with plain JDBC. Let s assume that you aren t sold on JTA and that you want to continue using the Hibernate Transaction API to keep your code runnable in Java SE and with managed Java EE services, without any code changes. To deploy the previous code
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

CHAPTER 10 Transactions and concurrency Usually, you also

Tuesday, September 18th, 2007

CHAPTER 10 Transactions and concurrency Usually, you also have to exit the application after you close the Session following an exception, although there are some exceptions (for example, Stale- ObjectStateException) that naturally lead to a new attempt (possibly after interacting with the application user again) in a new Session. Because these are closely related to conversations and concurrency control, we ll cover them later. FAQ Can I use exceptions for validation? Some developers get excited once they see how many fine-grained exception types Hibernate can throw. This can lead you down the wrong path. For example, you may be tempted to catch the ConstraintViolationException for validation purposes. If a particular operation throws this exception, why not display a (customized depending on the error code and text) failure message to application users and let them correct the mistake? This strategy has two significant disadvantages. First, throwing unchecked values against the database to see what sticks isn t the right strategy for a scalable application. You want to implement at least some data-integrity validation in the application layer. Second, all exceptions are fatal for your current unit of work. However, this isn t how application users will interpret a validation error they expect to still be inside a unit of work. Coding around this mismatch is awkward and difficult. Our recommendation is that you use the fine-grained exception types to display better looking (fatal) error messages. Doing so helps you during development (no fatal exceptions should occur in production, ideally) and also helps any customer-support engineer who has to decide quickly if it s an application error (constraint violated, wrong SQL executed) or if the database system is under load (locks couldn t be acquired). Programmatic transaction demarcation in Java SE with the Hibernate Transaction interface keeps your code portable. It can also run inside a managed environment, when a transaction manager handles the database resources. Programmatic transactions with JTA A managed runtime environment compatible with Java EE can manage resources for you. In most cases, the resources that are managed are database connections, but any resource that has an adaptor can integrate with a Java EE system (messaging or legacy systems, for example). Programmatic transaction demarcation on those resources, if they re transactional, is unified and exposed to the developer with JTA; javax.transaction.UserTransaction is the primary interface to begin and end transactions. The common managed runtime environment is a Java EE application server. Of course, application servers provide many more services, not only management of resources. Many Java EE services are modular installing an application server
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.