You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@servicemix.apache.org by watermelonjam <da...@linux.ca> on 2011/11/22 20:50:40 UTC

JPA EntityManager not properly injected for annotation-based config

While developing a JPA-backed CXF web service for SMX4 (v
2011.01.0-fuse-00-00), I discovered some odd behaviour with respect to the
injection of a thread-bound EntityManager.  I originally had service layer,
persistence unit and datasource in separate bundles, but the odd behaviour
occurs even when all layers exist in the same OSGi bundle.  This behaviour
does NOT occur during unit testing.  The problem is that the transaction is
created at the service layer:

@Service("messageService")
public class MessageServiceImpl implements MessageService {
	@Autowired
	private MessageDAO messageDAO;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void save(Message message) {
		messageDAO.save(message);
	}
}

...and propagated at the persistence layer:

@Repository("messageDAO")
public class SpringJpaMessageDAO implements MessageDAO {
	@PersistenceContext
	private EntityManager em;
	
	@Override
	@Transactional(propagation = Propagation.MANDATORY, readOnly = false)
	public void save(Message message) {
		em.persist(message);
	}
}

The call to persist() results in a NullPointerException for the
EntityManager.  This is despite the fact that the logs clearly indicate
Spring has found an active transaction and a thread-bound EntityManager:

18:56:09,469 | DEBUG | xtenderThread-17 | JpaTransactionManager            |
101 - org.springframework.transaction - 3.0.5.RELEASE | Creating new
transaction with name [org.example.service.MessageServiceImpl.save]:
PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
18:56:09,474 | DEBUG | xtenderThread-17 | JpaTransactionManager            |
101 - org.springframework.transaction - 3.0.5.RELEASE | Opened new
EntityManager [org.hibernate.ejb.EntityManagerImpl@1241201a] for JPA
transaction
18:56:09,490 | DEBUG | xtenderThread-17 | JpaTransactionManager            |
101 - org.springframework.transaction - 3.0.5.RELEASE | Exposing JPA
transaction as JDBC transaction
[org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@b6dcd37]
18:56:09,490 | TRACE | xtenderThread-17 | ransactionSynchronizationManager |
101 - org.springframework.transaction - 3.0.5.RELEASE | Bound value
[org.springframework.jdbc.datasource.ConnectionHolder@55bbdbbd] for key
[org.apache.commons.dbcp.BasicDataSource@7d22f244] to thread
[SpringOsgiExtenderThread-17]
18:56:09,490 | TRACE | xtenderThread-17 | ransactionSynchronizationManager |
101 - org.springframework.transaction - 3.0.5.RELEASE | Bound value
[org.springframework.orm.jpa.EntityManagerHolder@50e7a7f2] for key
[org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@18c32a5d]
to thread [SpringOsgiExtenderThread-17]
18:56:09,490 | TRACE | xtenderThread-17 | ransactionSynchronizationManager |
101 - org.springframework.transaction - 3.0.5.RELEASE | Initializing
transaction synchronization
18:56:09,490 | TRACE | xtenderThread-17 | TransactionInterceptor           |
101 - org.springframework.transaction - 3.0.5.RELEASE | Getting transaction
for [org.example.service.MessageServiceImpl.save]
18:56:09,490 | DEBUG | xtenderThread-17 | tationTransactionAttributeSource |
101 - org.springframework.transaction - 3.0.5.RELEASE | Adding transactional
method 'save' with attribute: PROPAGATION_MANDATORY,ISOLATION_DEFAULT; ''
18:56:09,491 | TRACE | xtenderThread-17 | ransactionSynchronizationManager |
101 - org.springframework.transaction - 3.0.5.RELEASE | Retrieved value
[org.springframework.orm.jpa.EntityManagerHolder@50e7a7f2] for key
[org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@18c32a5d]
bound to thread [SpringOsgiExtenderThread-17]
18:56:09,491 | DEBUG | xtenderThread-17 | JpaTransactionManager            |
101 - org.springframework.transaction - 3.0.5.RELEASE | Found thread-bound
EntityManager [org.hibernate.ejb.EntityManagerImpl@1241201a] for JPA
transaction
18:56:09,491 | TRACE | xtenderThread-17 | ransactionSynchronizationManager |
101 - org.springframework.transaction - 3.0.5.RELEASE | Retrieved value
[org.springframework.jdbc.datasource.ConnectionHolder@55bbdbbd] for key
[org.apache.commons.dbcp.BasicDataSource@7d22f244] bound to thread
[SpringOsgiExtenderThread-17]
18:56:09,491 | DEBUG | xtenderThread-17 | JpaTransactionManager            |
101 - org.springframework.transaction - 3.0.5.RELEASE | Participating in
existing transaction
18:56:09,491 | TRACE | xtenderThread-17 | TransactionInterceptor           |
101 - org.springframework.transaction - 3.0.5.RELEASE | Getting transaction
for [org.example.persist.SpringJpaMessageDAO.save]
18:56:09,491 | TRACE | xtenderThread-17 | TransactionInterceptor           |
101 - org.springframework.transaction - 3.0.5.RELEASE | Completing
transaction for [org.example.persist.SpringJpaMessageDAO.save] after
exception: java.lang.NullPointerException
18:56:09,491 | TRACE | xtenderThread-17 | RuleBasedTransactionAttribute    |
101 - org.springframework.transaction - 3.0.5.RELEASE | Applying rules to
determine whether transaction should rollback on
java.lang.NullPointerException
18:56:09,491 | TRACE | xtenderThread-17 | RuleBasedTransactionAttribute    |
101 - org.springframework.transaction - 3.0.5.RELEASE | Winning rollback
rule is: null
18:56:09,491 | TRACE | xtenderThread-17 | RuleBasedTransactionAttribute    |
101 - org.springframework.transaction - 3.0.5.RELEASE | No relevant rollback
rule found: applying default rules
18:56:09,491 | DEBUG | xtenderThread-17 | JpaTransactionManager            |
101 - org.springframework.transaction - 3.0.5.RELEASE | Participating
transaction failed - marking existing transaction as rollback-only
18:56:09,491 | DEBUG | xtenderThread-17 | JpaTransactionManager            |
101 - org.springframework.transaction - 3.0.5.RELEASE | Setting JPA
transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1241201a]
rollback-only
18:56:09,491 | TRACE | xtenderThread-17 | TransactionInterceptor           |
101 - org.springframework.transaction - 3.0.5.RELEASE | Completing
transaction for [org.example.service.MessageServiceImpl.save] after
exception: java.lang.NullPointerException
18:56:09,491 | TRACE | xtenderThread-17 | RuleBasedTransactionAttribute    |
101 - org.springframework.transaction - 3.0.5.RELEASE | Applying rules to
determine whether transaction should rollback on
java.lang.NullPointerException
18:56:09,491 | TRACE | xtenderThread-17 | RuleBasedTransactionAttribute    |
101 - org.springframework.transaction - 3.0.5.RELEASE | Winning rollback
rule is: null
18:56:09,491 | TRACE | xtenderThread-17 | RuleBasedTransactionAttribute    |
101 - org.springframework.transaction - 3.0.5.RELEASE | No relevant rollback
rule found: applying default rules
18:56:09,491 | DEBUG | xtenderThread-17 | JpaTransactionManager            |
101 - org.springframework.transaction - 3.0.5.RELEASE | Initiating
transaction rollback
18:56:09,491 | DEBUG | xtenderThread-17 | JpaTransactionManager            |
101 - org.springframework.transaction - 3.0.5.RELEASE | Rolling back JPA
transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1241201a]
18:56:09,492 | TRACE | xtenderThread-17 | ransactionSynchronizationManager |
101 - org.springframework.transaction - 3.0.5.RELEASE | Clearing transaction
synchronization
18:56:09,492 | TRACE | xtenderThread-17 | ransactionSynchronizationManager |
101 - org.springframework.transaction - 3.0.5.RELEASE | Removed value
[org.springframework.orm.jpa.EntityManagerHolder@50e7a7f2] for key
[org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@18c32a5d]
from thread [SpringOsgiExtenderThread-17]
18:56:09,492 | TRACE | xtenderThread-17 | ransactionSynchronizationManager |
101 - org.springframework.transaction - 3.0.5.RELEASE | Removed value
[org.springframework.jdbc.datasource.ConnectionHolder@55bbdbbd] for key
[org.apache.commons.dbcp.BasicDataSource@7d22f244] from thread
[SpringOsgiExtenderThread-17]
18:56:09,492 | DEBUG | xtenderThread-17 | JpaTransactionManager            |
101 - org.springframework.transaction - 3.0.5.RELEASE | Closing JPA
EntityManager [org.hibernate.ejb.EntityManagerImpl@1241201a] after
transaction
...
Caused by: java.lang.NullPointerException
	at
org.example.persist.SpringJpaMessageDAO.save(SpringJpaMessageDAO.java:22)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.6.0_26]
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)[:1.6.0_26]
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)[:1.6.0_26]
	at java.lang.reflect.Method.invoke(Method.java:597)[:1.6.0_26]
	at
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)[87:org.springframework.aop:3.0.5.RELEASE]
	at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)[87:org.springframework.aop:3.0.5.RELEASE]
	at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)[87:org.springframework.aop:3.0.5.RELEASE]
	at
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)[101:org.springframework.transaction:3.0.5.RELEASE]
	at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[87:org.springframework.aop:3.0.5.RELEASE]
	at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)[87:org.springframework.aop:3.0.5.RELEASE]
	at $Proxy172.save(Unknown Source)
	at org.example.service.MessageServiceImpl.save(MessageServiceImpl.java:36)

I have a maven sample project that exhibits the behaviour, available on
request.

--
View this message in context: http://servicemix.396122.n5.nabble.com/JPA-EntityManager-not-properly-injected-for-annotation-based-config-tp5014530p5014530.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.

Re: cxf-provider with SSL encryption

Posted by Freeman Fang <ff...@fusesource.com>.
Hi,

We have a testcase which demonstrate how to configure cxf bc provider  
with SSL, take a look at[1], also the configuration file used for this  
testcase[2],[3]
[1]https://svn.apache.org/repos/asf/servicemix/components/trunk/bindings/servicemix-cxf-bc/src/test/java/org/apache/servicemix/cxfbc/ws/security/CxfBcProviderHttpsTest.java
[2]https://svn.apache.org/repos/asf/servicemix/components/trunk/bindings/servicemix-cxf-bc/src/test/resources/org/apache/servicemix/cxfbc/ws/security/provider.xml
[3]https://svn.apache.org/repos/asf/servicemix/components/trunk/bindings/servicemix-cxf-bc/src/test/resources/org/apache/servicemix/cxfbc/ws/security/provider/WibbleClient.xml

Freeman
On 2011-12-9, at 下午9:25, Rafal Janik wrote:

> Hi
>
>
>
> I'm a little bit confused with configuring cxf-provider with SSL  
> encryption.
> In my servicemix there is a very simple proxy :
>
> <cxfbc:consumer wsdl="classpath:MyService.wsdl"
>                    targetEndpoint="HealthIndicatorImplPort"
>                    targetService="mynamespace:MyService"
>                     
> targetInterface="mynamespace:HealthIndicatorImplPort"
>                    endpoint="HealthIndicatorImplPortEndpoint"
>                    service="mynamespace:MyService"
>                    locationURI="http://0.0.0.0:8190/robust/ 
> MyService/">
>
>
> </cxfbc:consumer>
>
>
> <cxfbc:provider wsdl="classpath:MyService.wsdl"
>                    locationURI="https://my.service.com/MyService"
>                     
> interfaceName="mynamespace:HealthIndicatorImplPort2"
>                    service="mynamespace:MyService"
>                    endpoint="HealthIndicatorImplPort">
>
> </cxfbc:provider>
>
> The ws mentioned in cxf:bcprovider has changed and now it just  
> support SSL encryption.
> The cert was imported into created jks file.
>
> I want to leave the cxf-consumer without ssl and accept the cert  
> just for provider - I don't want the smx client see any changes in  
> API.
>
> Where and how I should configure the provider? With busCfg or in  
> cxf.xml? And how?  I've read many examples, blogs documentations but  
> it isn't working for me - probably I'm doing something wrong or I  
> don't understand how it should work...
>
>
> Please help me :)
>
> Thanks in advance.
>
>
> regards
>
>
> rafal
>
>
>
>
>
>
>
>
>
>

---------------------------------------------
Freeman Fang

FuseSource
Email:ffang@fusesource.com
Web: fusesource.com
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com










Re: cxf-provider with SSL encryption

Posted by Rafal Janik <ra...@softwaremind.pl>.
sorry for the previous mail, wrong topic,

cxf-provider with SSL encryption

Posted by Rafal Janik <ra...@softwaremind.pl>.
Hi



I'm a little bit confused with configuring cxf-provider with SSL encryption.
In my servicemix there is a very simple proxy :

<cxfbc:consumer wsdl="classpath:MyService.wsdl"
                     targetEndpoint="HealthIndicatorImplPort"
                     targetService="mynamespace:MyService"
                     targetInterface="mynamespace:HealthIndicatorImplPort"
                     endpoint="HealthIndicatorImplPortEndpoint"
                     service="mynamespace:MyService"
                     locationURI="http://0.0.0.0:8190/robust/MyService/">


</cxfbc:consumer>


<cxfbc:provider wsdl="classpath:MyService.wsdl"
                     locationURI="https://my.service.com/MyService"
                     interfaceName="mynamespace:HealthIndicatorImplPort2"
                     service="mynamespace:MyService"
                     endpoint="HealthIndicatorImplPort">

</cxfbc:provider>

The ws mentioned in cxf:bcprovider has changed and now it just support 
SSL encryption.
The cert was imported into created jks file.

I want to leave the cxf-consumer without ssl and accept the cert just 
for provider - I don't want the smx client see any changes in API.

Where and how I should configure the provider? With busCfg or in 
cxf.xml? And how?  I've read many examples, blogs documentations but it 
isn't working for me - probably I'm doing something wrong or I don't 
understand how it should work...


Please help me :)

Thanks in advance.


regards


rafal











Re: JPA EntityManager not properly injected for annotation-based config

Posted by Ioannis Canellos <io...@gmail.com>.
If this works for you, but you can't use it because of conflicts with other
things you need.
The use complie time weaving of the spring-aspects, when you are building
this artifact.

-- 
*Ioannis Canellos*
*
FuseSource <http://fusesource.com>

**
Blog: http://iocanel.blogspot.com
**
Apache Karaf <http://karaf.apache.org/> Committer & PMC
Apache Camel <http://camel.apache.org/> Committer
Apache ServiceMix <http://servicemix.apache.org/>  Committer
Apache Gora <http://incubator.apache.org/gora/> Committer
Apache DirectMemory <http://incubator.apache.org/directmemory/> Committer
*

Re: JPA EntityManager not properly injected for annotation-based config

Posted by watermelonjam <da...@linux.ca>.
Thanks for your reply.  Here's the spring context config:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
						http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">

	<context:annotation-config />

	<context:component-scan base-package="org.example" />

	<tx:annotation-driven transaction-manager="transactionManager" />

	<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>

	<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager" ref="transactionManager" />
	</bean>

	<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>

	<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter" ref="jpaAdapter" />
		<property name="persistenceUnitName" value="example" />
	</bean>

	<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
		<property name="showSql" value="true" />
		<property name="generateDdl" value="true" />
		<property name="databasePlatform"
value="org.hibernate.dialect.HSQLDialect" />
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" scope="singleton">
		<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
		<property name="url" value="jdbc:hsqldb:mem:smx4_jpa" />
		<property name="username" value="sa" />
		<property name="password" value="" />
	</bean>
</beans>

Using proxy-target-class="true" seems to work on my trivial example, but I
suspect it may cause issues with a WSDL-first JAX-WS web service.  I'll work
on extending the example to verify.


--
View this message in context: http://servicemix.396122.n5.nabble.com/JPA-EntityManager-not-properly-injected-for-annotation-based-config-tp5014530p5049727.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.

Re: JPA EntityManager not properly injected for annotation-based config

Posted by Ioannis Canellos <io...@gmail.com>.
Sorry for the late response, I've seen in the past similar issues with
injection and proxies.

Sometimes changing the spring aop configuration and forcing the use of
cglib proxy (proxy-target-classes=true) might solve this issue.
In other cases, its easier to force the compile time weaving of the
transactional aspect.

If you want a more clear response, I would like to have a glimpse at your
spring context.

You also might find this useful:
http://iocanel.blogspot.com/2010/06/spring-aop-and-reflection-pitfalls.html


-- 
*Ioannis Canellos*
*
FuseSource <http://fusesource.com>

**
Blog: http://iocanel.blogspot.com
**
Apache Karaf <http://karaf.apache.org/> Committer & PMC
Apache Camel <http://camel.apache.org/> Committer
Apache ServiceMix <http://servicemix.apache.org/>  Committer
Apache Gora <http://incubator.apache.org/gora/> Committer
Apache DirectMemory <http://incubator.apache.org/directmemory/> Committer
*