jEmbedded & Spring-Transaction

3 views
Skip to first unread message

Adolfo Estevez

unread,
Nov 9, 2009, 12:30:38 PM11/9/09
to jEmbedded Discussion Group
Hi,

I've been having a look if the Spring's TX&AOP mechanish works with
jEmbedded as some of you couldn't use it (thanks Jokoul).

So I made some tests with the invoices-service, using the
configuration that Jokoul sent to the group. The problem I found is
the datasource that was loaded by @SpringContext was accessed by the
tx mechanism.

So what I did to make it work (in the invoices-service.xml):

- Export the whole container to the Spring Context instead just the
service:

<bean id="handler"
class="org.jsemantic.jembedded.support.spring.EmbeddedContainerRunner">
<property name="useContext" value="false"/>
<property name="annotatedClasses">
<list>

<value>org.jsemantic.services.examples.energy.invoice.InvoiceService</
value>
</list>
</property>
</bean>

This is a new version of the runner, note the property useContext=
false, this is needed to block the jEmbedded container to access the
SpringContext directly and create a circular reference as we are using
the @SpringContext to load the persistence, and therefore the
DataSource. The EmbeddedContainerRunner otherwise will try to create
the datasource itself.


- Created a new export class: ResouceExported to allow to export any
resource from jEmbedded to the context (in this case the DataSource
and the InvoicesService):

<bean id="dataSource"
class="org.jsemantic.jembedded.support.spring.exporter.ResourceExporter"
autowire="autodetect" depends-on="handler">
<property name="id" value="dataSource"/>
</bean>

<bean id="invoicesService"
class="org.jsemantic.jembedded.support.spring.exporter.ResourceExporter"
autowire="autodetect" depends-on="handler">
<property name="id" value="invoicesService"/>
</bean>

This way the two resources from jEmbedded would be available for
Spring.

- Then added the tx/aop config as jokoul did.

Works perfectly, I modified the DAO in order to insert two rows and
the data was uncomitted.

I uploaded the files to the group.

Hope this helps

Adolfo

mens jamie

unread,
Nov 9, 2009, 9:05:27 PM11/9/09
to jembedded-dis...@googlegroups.com

Hi Adolfo,would you please publish your test file,I still used the existed InvoiceServiceTest with @SpringRepository(configurationFile = "META-INF/invoices-service/invoices-service.xml"),but still no correct result.

Adolfo Estevez

unread,
Nov 9, 2009, 11:40:19 PM11/9/09
to jembedded-dis...@googlegroups.com
Hi,

yeah I will

Adolfo

2009/11/10 mens jamie <jamie...@gmail.com>

Hi Adolfo,would you please publish your test file,I still used the existed InvoiceServiceTest with @SpringRepository(configurationFile = "META-INF/invoices-service/invoices-service.xml"),but still no correct result.

Adolfo Estevez

unread,
Nov 10, 2009, 12:03:27 AM11/10/09
to jEmbedded Discussion Group
Here it is the log I get, as I also modified the DAO so some data was
inserted.

take care

Adolfo

2009-11-09 18:03:06,390 DEBUG
[org.springframework.jdbc.support.SQLErrorCodesFactory] - Looking up
default SQLErrorCodes for DataSource
[org.jsemantic.services.jdbc.service.datasource.derby.DerbyDataSource@1f71773]
2009-11-09 18:03:06,406 TRACE
[org.springframework.transaction.support.TransactionSynchronizationManager]
- Retrieved value
[org.springframework.jdbc.datasource.ConnectionHolder@415b46] for key
[org.jsemantic.services.jdbc.service.datasource.derby.DerbyDataSource@1f71773]
bound to thread [main]
2009-11-09 18:03:06,406 TRACE
[org.springframework.transaction.support.TransactionSynchronizationManager]
- Retrieved value
[org.springframework.jdbc.datasource.ConnectionHolder@415b46] for key
[org.jsemantic.services.jdbc.service.datasource.derby.DerbyDataSource@1f71773]
bound to thread [main]
2009-11-09 18:03:06,406 DEBUG
[org.springframework.jdbc.support.SQLErrorCodesFactory] - Database
product name cached for DataSource
[org.jsemantic.services.jdbc.service.datasource.derby.DerbyDataSource@1f71773]:
name is 'Apache Derby'
2009-11-09 18:03:06,406 DEBUG
[org.springframework.jdbc.support.SQLErrorCodesFactory] - SQL error
codes for 'Apache Derby' found
2009-11-09 18:03:06,406 DEBUG
[org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator]
- Translating SQLException with SQL state '23502', error code '20000',
message [Column 'DESCRIPTION' cannot accept a NULL value.]; SQL was
[insert into tariff(tariff_type,description) values(?,?)] for task
[PreparedStatementCallback]
2009-11-09 18:03:06,421 TRACE
[org.springframework.transaction.interceptor.TransactionInterceptor] -
Completing transaction for
[org.jsemantic.services.examples.energy.invoice.InvoiceService.generateInvoice]
after exception:
org.springframework.dao.DataIntegrityViolationException:
PreparedStatementCallback; SQL [insert into tariff
(tariff_type,description) values(?,?)]; Column 'DESCRIPTION' cannot
accept a NULL value.; nested exception is java.sql.SQLException:
Column 'DESCRIPTION' cannot accept a NULL value.
2009-11-09 18:03:06,421 TRACE
[org.springframework.transaction.interceptor.RuleBasedTransactionAttribute]
- Applying rules to determine whether transaction should rollback on
org.springframework.dao.DataIntegrityViolationException:
PreparedStatementCallback; SQL [insert into tariff
(tariff_type,description) values(?,?)]; Column 'DESCRIPTION' cannot
accept a NULL value.; nested exception is java.sql.SQLException:
Column 'DESCRIPTION' cannot accept a NULL value.
2009-11-09 18:03:06,421 TRACE
[org.springframework.transaction.interceptor.RuleBasedTransactionAttribute]
- Winning rollback rule is: RollbackRuleAttribute with pattern
[RuntimeException]
2009-11-09 18:03:06,421 TRACE
[org.springframework.jdbc.datasource.DataSourceTransactionManager] -
Triggering beforeCompletion synchronization
2009-11-09 18:03:06,421 DEBUG
[org.springframework.jdbc.datasource.DataSourceTransactionManager] -
Initiating transaction rollback
2009-11-09 18:03:06,421 DEBUG
[org.springframework.jdbc.datasource.DataSourceTransactionManager] -
Rolling back JDBC transaction on Connection
[org.apache.derby.impl.jdbc.EmbedConnection30@21287198 (XID = 211),
(SESSIONID = 4), (DATABASE = test), (DRDAID = null) ]
2009-11-09 18:03:06,453 TRACE
[org.springframework.jdbc.datasource.DataSourceTransactionManager] -
Triggering afterCompletion synchronization
2009-11-09 18:03:06,453 TRACE
[org.springframework.transaction.support.TransactionSynchronizationManager]
- Clearing transaction synchronization
2009-11-09 18:03:06,453 TRACE
[org.springframework.transaction.support.TransactionSynchronizationManager]
- Removed value
[org.springframework.jdbc.datasource.ConnectionHolder@415b46] for key
[org.jsemantic.services.jdbc.service.datasource.derby.DerbyDataSource@1f71773]
from thread [main]
2009-11-09 18:03:06,453 DEBUG
[org.springframework.jdbc.datasource.DataSourceTransactionManager] -
Releasing JDBC Connection
[org.apache.derby.impl.jdbc.EmbedConnection30@21287198 (XID = 211),
(SESSIONID = 4), (DATABASE = test), (DRDAID = null) ] after
transaction
2009-11-09 18:03:06,453 DEBUG
[org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC
Connection to DataSource
2009-11-09 18:03:15,000 DEBUG
[org.springframework.jdbc.core.JdbcTemplate] - Executing SQL query
[select * from tariff]
2009-11-09 18:03:15,000 DEBUG
[org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC
Connection from DataSource
2009
On 10 nov, 05:40, Adolfo Estevez <aestevezjime...@gmail.com> wrote:
> Hi,
>
> yeah I will
>
> Adolfo
>
> 2009/11/10 mens jamie <jamie.m...@gmail.com>

mens jamie

unread,
Nov 10, 2009, 12:20:47 AM11/10/09
to jembedded-dis...@googlegroups.com

I run your SpringTest with error "Resource dataSource not found in the jEmbedded container",why.

Adolfo Estevez

unread,
Nov 10, 2009, 12:53:27 AM11/10/09
to jEmbedded Discussion Group
This is a exception launched by the ResourceExporter because it could
not find the DataSource in the jEmbedded Container.

Make sure you have this 2 lines in the InvoiceService:

@Container(instanceType=ContainerInstanceType.PROTOTYPE_VM)
@SpringRepository(configurationFile = "META-INF/invoices-service/
embedded-database-service/persistence-layer.xml")

The firs one it's to avoid problems with the Container created for the
Test, I suppose I will change the default to this instead creating
container by thread by default.

The datasource is created in the persistence-layer.xml.

With this the example should work I will upload the complete service,
but keep in mind I modified it to throw an exception.

Tell me if it worked

Adolfo

Adolfo Estevez

unread,
Nov 10, 2009, 12:56:29 AM11/10/09
to jEmbedded Discussion Group
You can put a try catch in the service invocation and then execute a
select, you will find that the data it's not there.

maybe you will need to add more dependencies to the pom, it should
work though.

Adolfo

On 10 nov, 06:20, mens jamie <jamie.m...@gmail.com> wrote:

mens jamie

unread,
Nov 10, 2009, 1:57:05 AM11/10/09
to jembedded-dis...@googlegroups.com

Sorry,I still can't let it work,the error just as previous. 

Adolfo Estevez

unread,
Nov 10, 2009, 2:10:31 AM11/10/09
to jembedded-dis...@googlegroups.com
Strange,  even with the complete service I uploaded?

Even this time I'm running it from Eclipse as you do.

Please post the log

Take care

Adolfo


On Tue, Nov 10, 2009 at 7:57 AM, mens jamie <jamie...@gmail.com> wrote:

Sorry,I still can't let it work,the error just as previous. 




jokoul webster

unread,
Nov 10, 2009, 5:37:51 PM11/10/09
to jembedded-dis...@googlegroups.com
Hi Adolfo,thanks a lot,I have got what I wanna,yeah,the declaration transaction is ok.
Hi Jamie,maybe you should add JdbcServiceImpl.class to InvoiceService @repository,or the example couldn't find the database.
 
JK.Webster
 

Adolfo Estevez

unread,
Nov 10, 2009, 11:10:16 PM11/10/09
to jEmbedded Discussion Group
Great, I will go on with the docs and will add the transaction stuff
as an example.

Take care

Adolfo
Reply all
Reply to author
Forward
0 new messages