Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRAILS-5080: Services circular references fail with transactional true but succeeds when false #1750

Closed
graemerocher opened this issue Sep 8, 2009 · 3 comments

Comments

@graemerocher
Copy link
Member

Original Reporter: fjanon
Environment: Grails 1.1.1, WinXP
Version: 1.1.1
Migrated From: http://jira.grails.org/browse/GRAILS-5080

Not sure it's a bug or a feature of Spring. I thought it's worth looking into it and documenting it.

In the end, the question is: can we create the services with transactional = false and then change transactional = true when we need it in a service method?

I create a new project with only 2 services referencing each other. "run-app" fails with:

2009-09-08 16:00:22,782 [main] ERROR context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceAService': Cannot create inner bean '(inner bean)' whil
e setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner b
ean)#3': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'se
rviceBService': Cannot create inner bean '(inner bean)' while setting bean property 'target'; nested exception is org.springframework.beans.factory.Be
anCreationException: Error creating bean with name '(inner bean)#3': Initialization of bean failed; nested exception is org.springframework.beans.fact
ory.BeanCurrentlyInCreationException: Error creating bean with name 'serviceAService': org.springframework.beans.factory.FactoryBeanNotInitializedExce
ption: FactoryBean is not fully initialized yet

I then change only ServiceAService:

boolean transactional = false

and "run-app" is happy.

class ServiceAService {

boolean transactional = false
def ServiceBService

def serviceMethod() {
    println "ServiceAService serviceMethod"
}

}

class ServiceBService {

boolean transactional = true
def ServiceAService

def serviceMethod() {
    println "ServiceBService serviceMethod"
}

}

Note that is transactional is true in ServiceA and false in ServiceB it fails (I guess because on the alphabetical order on creating services)

@graemerocher
Copy link
Member Author

graemerocher said:
in order to do circular references you have to implement InitializingBean as per the Spring recommendations. This is not a Grails bug, if you have further concerns raise in Spring JIRA

@graemerocher
Copy link
Member Author

graemerocher said:
You need to implement ApplicationContextAware and in your logic you need lookup your service from the applicationContext manually

@graemerocher
Copy link
Member Author

zergling_zzh said:
Why does grails not use lazy init on all reference to services and controllers? It will avoid circular references issue. Not all grails users are masters of Spring. It is really easy to get circular reference in big projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant