Hi Charlie
yes I've tried setting the flushmode to 'COMMIT' in that case
calling a dao.find(..) within the transaction does not fire a flush and
hence if there is any exception or problem during the transaction
nothing will be flushed to the DB. However, you might gate stale objects
during the transaction e.g if you did an update over a property and then
you query this object you just updated to the db it will come with the
old data. An additional side effect of the flushmode in 'COMMIT' it
could be that in very loong transaction a stack overflow might occurr as
everything is kept in memory
According to what I had understood, the fact of flushing data to the
database does not imply a commit and hence a full rollback should be
possible as the transaction commit is supposed to happen only at the
very end of the transaction. But in my case, somehow -may be something I
don't manage to spot that is missconfigured...- even at the time when
the jta transaction is not yet committed, it seems like the db
transaction it is -after a flush- and that's the reason I suppose that I
can see the data from MySQL Workbench and also the reason that the
rollback seems to have no effect at all. And there are so many layers
here that I can't see where the problem might be.. perhaps aries
jta/jpa, perhaps geronimo (the transaction manager that aries uses as
far as I understand) may be the jpa provider that I'm using -hibernate
4.3.11- or may be something wrong with the jdbc connection config
I will try to do a very very simple db application using
aries/hibernate/mysql with the minimum running bundles to see whether I
still have this issue so that I can ensure that I do not have any other
potential problem like a conflicting bundle or something. Although I
hope not because everything seems to be up and running and with no
exceptions at all.
Best
Pablo
On 25/08/2016 11:11 AM, Charlie Mordant wrote:
Hi Pablo,
What about removing the flush/flushmode? I'm not a JPA expert, but
calling flush will definitely put the data in the db, and
flushmode='auto' will call it at every find:
https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/FlushMode.html
Regards,
2016-08-24 17:15 GMT+02:00 Pablo Gómez Pérez <[email protected]
<mailto:[email protected]>>:
Hi all,
I need again from your help, I'm facing the following issue,
Having a method annotated as transactional that samples data to
the database and assuming that we start with an Empty database
@Transactional(value = TxType.REQUIRED, rollbackOn = {
Exception.class })
public void sample() {
dao.persist(....)
dao.merge(....)
//here a find so that we make the provider to do a
flush to the DB as the flushmode is configured as AUTO
dao.find(.....)
//Now if I open the MySQLWorkbench I can see the
persisted/updated data even though the transaction it is not yet
commited
//Do more operations
persist(....)
//We throw an exception to force a rollback
throw new RuntimeException("I want a rollback! :)")
}
Where DAO methods are of course annotated with @Transactional
alike the Sampler with REQUIRED and rollbackOn Exception.class.
And Of course the <tx:enable/> it is configured too in the DAO
blueprint.
As you can assume in the DAO there is the em injected as specified
in the docu:
@PersistenceContext(unitName = "managed-jpa")
private EntityManager entityManager;
Using MySQL InnoDB as follows
<?xml version="1.0" encoding="UTF-8"?>
<blueprint default-activation="eager"
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0
<http://www.osgi.org/xmlns/blueprint/v1.0.0>"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
<http://www.w3.org/2001/XMLSchema-instance>"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0
<http://aries.apache.org/xmlns/jpa/v2.0.0>"
xmlns:tx="http://aries.apache.org/xmlns/transactions/v2.0.0
<http://aries.apache.org/xmlns/transactions/v2.0.0>">
<bean id="dataSource"
class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
<property name="URL"
value="jdbc:mysql://localhost:3306/dbschema"/>
<property name="user" value="root" />
<property name="password" value="" />
</bean>
<service interface="javax.sql.DataSource" ref="dataSource">
<service-properties>
<entry key="osgi.jndi.service.name
<http://osgi.jndi.service.name>" value="jdbc/myds" />
</service-properties>
</service>
</blueprint>
And.. persistence unit
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence
<http://java.sun.com/xml/ns/persistence>"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
<http://www.w3.org/2001/XMLSchema-instance>"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
<http://java.sun.com/xml/ns/persistence>
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
<http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd>">
<persistence-unit name="managed-jpa" transaction-type="JTA">
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name
<http://osgi.jndi.service.name>=jdbc/myds)</jta-data-source>
<properties>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto"
value="create-drop" />
<property name="hibernate.archive.autodetection"
value="class" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="org.hibernate.flushMode" value="AUTO"/>
</properties>
</persistence-unit>
</persistence>
Considering that I do have transactional annotations activated:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0
<http://www.osgi.org/xmlns/blueprint/v1.0.0>"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
<http://www.w3.org/2001/XMLSchema-instance>"
xmlns:tx="http://aries.apache.org/xmlns/transactions/v2.0.0
<http://aries.apache.org/xmlns/transactions/v2.0.0>"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0
<http://aries.apache.org/xmlns/jpa/v2.0.0>"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0
<http://www.osgi.org/xmlns/blueprint/v1.0.0>
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
<http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd>
http://aries.apache.org/xmlns/transactions/v2.0.0
<http://aries.apache.org/xmlns/transactions/v2.0.0>
http://aries.apache.org/schemas/transaction/transactionv20.xsd
<http://aries.apache.org/schemas/transaction/transactionv20.xsd>">
<jpa:enable />
<tx:enable/>
Additionally, lets assume that we have a Synchronization
registered to the transaction so that afterCompletion and
BeforeeCompletion are called.
So now my problem:
The transaction in before and after completion
has status 4 -rollback- as expected because of the exception I'm
throwing in the sampler, however there is no real rollback as I
can still see the data in the database -eg. using again the MySQL
Workbench- perhaps during the transaction the DB transaction was
commited when the flush was performed due to the call to 'find' in
the sampler. Actually for me it looks that somehow there is an
autocommit when flush...
I was expecting that the fact of calling find during the
transaction -fires a flush- which does happen so that the find
returns data considering what already did happen during the
transaction. However, I wasn't expecting to see this data already
in the MySQLWorkbench as that is a different transaction. But more
importantly I was expecting the database to be empty at the end of
the transaction due to the provoked rollback
These are the jpa jta bundles I have
START LEVEL 6
ID|State |Level|Name
25|Active | 2|Apache Aries JPA blueprint (2.4.0)|2.4.0
38|Active | 2|Apache Aries JPA Container API (2.4.0)|2.4.0
40|Active | 2|Apache Aries JPA container (2.4.0)|2.4.0
50|Active | 2|Apache Aries JPA support (2.4.0)|2.4.0
g! lb transaction
START LEVEL 6
ID|State |Level|Name
23|Active | 2|Apache Aries Transaction Blueprint
(1.1.1)|1.1.1
68|Active | 4|javax.transaction API (1.2.0)|1.2.0
104|Active | 2|Apache Aries Transaction Manager (1.3.0)|1.3.0
138|Active | 2|Apache Aries Transaction Blueprint
(2.1.0)|2.1.0
I have running blueprint 1.1.1 and 2.1.0 but as noted in the
blueprint above, I'm using namespace 2.0 so the jpa blueprint
1.1.1 is not really needed but anyway I have it running for
testing purposes
Any hint about what I'm doing wrong?
Thank you!
regards
Pablo
--
WARNING: Computer viruses can be transmitted via email. The
recipient should check this email and any attachments for the
presence of viruses. The company accepts no liability for any
damage caused by any virus transmitted by this email. E-mail
transmission cannot be guaranteed to be secure or error-free as
information could be intercepted, corrupted, lost, destroyed,
arrive late or incomplete, or contain viruses. The sender
therefore does not accept liability for any errors or omissions in
the contents of this message, which arise as a result of e-mail
transmission.
Warning: Although the company has taken reasonable precautions to
ensure no viruses are present in this email, the company cannot
accept responsibility for any loss or damage arising from the use
of this email or attachments.
--
Charlie Mordant
Full OSGI/EE stack made with Karaf:
https://github.com/OsgiliathEnterprise/net.osgiliath.parent