persistence@glassfish.java.net

Direct DB updates are not visible in in JPA (even after clearing)

From: Adam Bien <abien_at_adam-bien.com>
Date: Sat, 12 Jul 2008 14:54:32 +0200

Hi All,

 

I ran into following problem: Direct changes in the database (SQL
updates) performed in the same Bean-method, and so transaction, are not
visible for the EntityManager (subsequent find).

Even invoking EntityManager#clear does not solve the problem. The
problem is strange, I suspect that the DB-connection from the datasource
does not participate in the TX. This would explain such behaviour.

 

Server: Glassfishv2u2

Database: OracleXE

 

The Bean looks like:

 

 

@Stateless

@Remote(VehicleManager.class)

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

public class VehicleManagerBean implements VehicleManager {

 

    @PersistenceContext

    private EntityManager entityManager;

    @Resource(mappedName = "jdbc/db1")

 * private DataSource dataSource;*

    @EJB

    private DataService dataService;

 

 

    public Vehicle findChangeDBClearFind(int id, String newColor) {

        Vehicle vehicle = this.find(id);

        try {

 

           //changes the color of the vehicle directly in the DB

     * this.changeVehiclesColor(id, newColor);*

* * } catch (Exception e) {

            throw new EJBException("Problem executing statement" + e, e);

        }

        this.entityManager.clear();

        return this.find(id);

    }

   private void changeVehiclesColor(int id, String color) throws Exception {

        Connection connection = this.dataSource.getConnection();

        Statement statement = connection.createStatement();

        String sqlStatement = "update vehicle set color=\'" + color +
"\' where id=" + id;

        System.out.println(sqlStatement);

        int updatedRecords = statement.executeUpdate(sqlStatement);

        if (updatedRecords != 1) {

            throw new IllegalStateException("Vehicle with id: " + id + "
cannot be updated!");

        }

        connection.close();

        statement.close();

    }

 

 

 

I deployed the same code to WLS 10 and it worked - the changes were
visible. I suppose it is probably a configuration problem. I tried with
different DataSource configurations - with no effect. I attached my
current configuration as well (screenshot).

 

Hopefully I overlooked something. If not - I will file it as a bug.

 

Thank you in advance,

 

regards,

 

adam bien