Showing posts with label Solution. Show all posts
Showing posts with label Solution. Show all posts

Saturday, December 22, 2007

XA Two Phase Transactions doesn't work

When you use XA or in it's second name Two Phase Commit Transaction and the transaction doesn't work it can be cause by one of the following reasons:

  • You didn't declare transaction manager in your application server or in your spring XML configuration file
  • You declared transaction manager but no annotation was defined for the EJB or the annotation was defined NOT_SUPPORTED
  • There is annotation but if you call from some method in the EJB to another method in the same EJB the second method can't open transaction since it is a local call which doesn't execute through the j2ee engine interpreter and therefore not transaction is opened. A Possible solution for that is to use UserTransaction and open the transaction pragmatically or to move the second method to another EJB.

Wednesday, December 12, 2007

Mapping Hibernate to DB View

There is a problem to map POJO to DB View using Hibernate if you are using also HBM2DLL.
In this case the HBM2DLL would create for each HBM mapping a corresponsing DB table.

To solve the problem -

  • Write HBM as usual
  • Add deletion of the DB table as follow:


DROP TABLE MY_TABLE;


  • Create the view as follow (also in databse object tag)
CREATE VIEW MY_VIEW AS SELECT ....;

Hibernate would write data and read data now with the DB view.

Tuesday, December 11, 2007

ClassCastException Solution

The obvious cause for ClassCastException is that you try to cast from one class to another class but the other class is not a parent of the class you try to cast, but there are other cases in which ClassCastException can happen:

AClass aObject = (AClass) bObject;

- bObject doesn't inherit from AClass or it doesn't AClass instance.

- bObject inherit from AClass or it is AClass instance, however bObject was loaded by a different class loader, In this case the JVM consider bObject as a different class, the solution is to load AClass only in one of the class loaders, the same class shouldn't be loaded by two different class loaders.

For example:
Object a1 = new A(); // Load by classLoader x
Object a2 = new A(); // Load by classLoader y

a1 = (A) a2; // Would throw ClassCastException

ClassNotFoundException Solution

Here is a list of possible problems which would may case ClassNotFoundException and the solution for this problems

  1. The class doesn't exists in the classpath, You can add it to your classpath by either click on properties of the project and add it to the java build path or you can choose run->classpath->and add the jar.
  2. The class exists in the classpath, but it has a static section in the beginning of the class which throws an exception in this case the class doesn't create by the classloader, the solution is to fix the exception thrown in the static section or add try catch.
  3. The class exists but it is loaded by a different classloader, for example you have two EAR files in which one need to access another one, The solution is to add reference between this two EAR files, or making one of the class loaders be the parent of the other class loader

Links

 
RSS Feeds Submission Directory