How to remove a dynamically registered domain class from current session.

298 views
Skip to first unread message

Main Pal

unread,
Apr 10, 2012, 3:36:17 AM4/10/12
to grails-dynamic-do...@googlegroups.com
I'm using this plugin in my Grails Application to load dynamically created domains. When I want to remove any dynamically loaded domain, I delete the table from database for that domain using SQL. But, when I register a new domain it creates table for the deleted domain, as, I believe, it doesn't remove the dynamically loaded domain from the session.

How could I remove it from the session so that the table doesn't get recreated?

Thanks in Advance,
Main Pal

Chee Kin Lim

unread,
Apr 10, 2012, 11:07:17 AM4/10/12
to grails-dynamic-do...@googlegroups.com

Main Pal

unread,
Apr 11, 2012, 3:29:13 AM4/11/12
to grails-dynamic-do...@googlegroups.com
Hi  Chee Kin,

Thanks for your reply and efforts.
I looked into this and tried to use it, but, I'm running my application on Grails-1.3.7 which has springFramework-2.0.
The class suggested by you is a great help :). Could you please suggest me how to update springFramework to 2.5 version without updating Grails version.

Thank you,
Main Pal

Chee Kin Lim

unread,
Apr 11, 2012, 10:07:32 AM4/11/12
to grails-dynamic-do...@googlegroups.com
Hi Main Pal,

I think it is not advisable to upgrade SpringFramework which is Grails internal to 2.5, in fact I didn't have any experience doing so. Please check whether SpringFramework 2.0 support the API I proposed.

Best regards,
Chee Kin

Main Pal

unread,
Apr 12, 2012, 2:09:02 AM4/12/12
to Grails Dynamic Domain Class Plugin
Hi  Chee Kin,

I actually tried using that removeBeanDefinition function you
suggested, but it's not available in SpringFramework 2.0. This is the
only reason I ask your suggestion to update it to 2.5. Anyways, I need
to find out how to get this support. I'll try adding a jar file having
GenericApplicationContext from 2.5 framework into my application. I
believe if it doesn't break anything we should go with it. :)

Thanks a ton for you efforts and help.
Main Pal

On Apr 11, 7:07 pm, Chee Kin Lim <limchee...@vobject.com> wrote:
> Hi Main Pal,
>
> I think it is not advisable to upgrade SpringFramework which is Grails
> internal to 2.5, in fact I didn't have any experience doing so. Please
> check whether SpringFramework 2.0 support the API I proposed.
>
> Best regards,
> Chee Kin
>
>
>
>
>
>
>
> On Wed, Apr 11, 2012 at 3:29 PM, Main Pal <main...@oneappcloud.com> wrote:
> > Hi  Chee Kin,
>
> > Thanks for your reply and efforts.
> > I looked into this and tried to use it, but, I'm running my application on
> > Grails-1.3.7 which has springFramework-2.0.
> > The class suggested by you is a great help :). Could you please suggest me
> > how to update springFramework to 2.5 version without updating Grails
> > version.
>
> > Thank you,
> > Main Pal
>
> > On Tuesday, April 10, 2012 8:37:17 PM UTC+5:30, limcheekin wrote:
>
> >> Hi Main Pal,
>
> >> I did some research, try this ...http://static.springsource.**
> >> org/spring/docs/2.5.x/api/org/**springframework/context/**support/**
> >> GenericApplicationContext.**html#removeBeanDefinition%**
> >> 28java.lang.String%29<http://static.springsource.org/spring/docs/2.5.x/api/org/springframew...>
>
> >> Let's see whether it works.
>
> >> Best regards,
> >> Chee Kin
>
> >> On Tue, Apr 10, 2012 at 3:36 PM, Main Pal <main...@oneappcloud.com>wrote:
>
> >>> I'm using this plugin in my Grails Application to load dynamically
> >>> created domains. When I want to remove any dynamically loaded domain, I
> >>> delete the table from database for that domain using SQL. But, when I
> >>> register a new domain it creates table for the deleted domain, as, I
> >>> believe, it doesn't remove the dynamically loaded domain from the session.
>
> >>> How could I remove it from the session so that the table doesn't get
> >>> recreated?
>
> >>> Thanks in Advance,
> >>> Main Pal
>
> > On Tuesday, April 10, 2012 8:37:17 PM UTC+5:30, limcheekin wrote:
>
> >> Hi Main Pal,
>
> >> I did some research, try this ...http://static.springsource.**
> >> org/spring/docs/2.5.x/api/org/**springframework/context/**support/**
> >> GenericApplicationContext.**html#removeBeanDefinition%**
> >> 28java.lang.String%29<http://static.springsource.org/spring/docs/2.5.x/api/org/springframew...>
>
> >> Let's see whether it works.
>
> >> Best regards,
> >> Chee Kin
>

callie

unread,
Apr 22, 2012, 10:34:02 PM4/22/12
to Grails Dynamic Domain Class Plugin
Hi,

I was wondering if you had progress with this issue? Ran into it some
time ago but the SpringFramework version of our Grails 1.3.7 was 3.0
instead of 2.0. We did call removeBeanDefinition but the deleted
tables (also via SQL) still got recreated once another domain is
registered.

Thoughts? :)
callie

Chee Kin Lim

unread,
Apr 23, 2012, 2:13:31 AM4/23/12
to grails-dynamic-do...@googlegroups.com
Hi Callie,

I have no idea at the moment and need more time (resources which I short of currently...) to do additional research to solve this problem. Is this issue critical to you?

By the way, Grails 2.0.x have a great support on NoSQL technologies by adopted Spring Data, you may considered to switch to NoSQL data source as it was backed by SpringSource development team.

Do you mind to share how are you using the dynamic-domain-class plugin?

Hope this help.

Best regards,
Chee Kin

callie

unread,
Apr 24, 2012, 9:05:01 PM4/24/12
to Grails Dynamic Domain Class Plugin
Hi Chee Kin,

Basically, what we do is we let the user define the fields they want
to use. Then, after building the appropriate domain class, it's
compiled and deployed using the plugin. However, as I've mentioned
before in a separate post, we were running into issues with 1:n
relationships (since the plugin basically does m:n relationships)
since there are limited handling for some functions (e.g. delete, save
of records).

In the course of discovering those, we then ran into the delete
problem also for the class itself since, for some reason, even if the
bean reference is deleted/removed, a reference to the domain class is
kept in the application itself (hence, if you do something like
grailsApplication.getDomainClass(xxx) after doing a
removeBeanDefinition, the class is still there... until you restart
the app server, which is something that we're avoiding since we're
trying to avoid any downtime for the web app).

I know that Grails 2.0 has improved in a way that it uses jvm agents
instead of class loaders for the reloading of domain classes. However,
I'm not very familiar with that type of implementation and upgrading
to 2.0 is not yet an option at this point.

Any thoughts are welcome :)

Thanks,
callie

Chee Kin Lim

unread,
Apr 24, 2012, 10:25:49 PM4/24/12
to grails-dynamic-do...@googlegroups.com
Hi Callie,

Good morning! Thanks for further sharing.

Remove the bean from application context is not enough, you need to remove the domain class from SessionFactory too as SessionFactory was the class responsible for schema creation. Please see http://code.google.com/p/grails-dynamic-domain-class-plugin/source/browse/trunk/src/groovy/org/grails/dynamicdomain/DynamicDomainService.groovy#127 for the existing way of replacing the old SessionFactory to the new one.


Hope this help.

Best regards,
Chee Kin



callie

unread,
Jul 12, 2012, 5:43:41 AM7/12/12
to grails-dynamic-do...@googlegroups.com
Oops, sorry it took so long to reply; had other priorities since April.

Thanks! Hmm.. Can you check if I'm doing this right?

1. We deleted the table via SQL...
2. We delete the bean from context with something like:

def dc = // get the domain class
def ctx = grailsApplication.mainContext
if (ctx.isBeanNameInUse(dc.fullName)) {
    BeanDefinitionRegistry factory = (BeanDefinitionRegistry) ctx.getAutowireCapableBeanFactory()
    factory.removeBeanDefinition dc.fullName
    factory.removeBeanDefinition "${dc.fullName}DomainClass"
    factory.removeBeanDefinition "${dc.fullName}PersistentClass"
    factory.removeBeanDefinition "${dc.fullName}Validator"
}

3. Call the update:

def dds = grailsApplication.mainContext.dynamicDomainService
dds.updateSessionFactory grailsApplication.mainContext

This is what I've tried so far... And although the table is deleted, after the 3rd step, the domain class can still be retrieved via grailsApplication.getDomainClass(xxx) :( Thoughts on this?

Thanks!

Main Pal Yadav

unread,
Jul 12, 2012, 7:08:31 AM7/12/12
to grails-dynamic-do...@googlegroups.com
Hey guys,

I also tried to remove this from sessionFactory but no success. Please post as soon as you find the solution.
I tried:
def dc = ApplicationHolder.application.getDomainClass(this.domainClass.name)
boolean isDC = false
if(dc)
isDC = true
def dcName = dc?.fullName
dc = null
if(dcName){
                                GenericApplicationContext gac = AH.application.mainContext
gac.removeBeanDefinition(dcName)
gac.removeBeanDefinition(dcName+"DomainClass")
gac.removeBeanDefinition(dcName+"PersistentClass")
gac.removeBeanDefinition(dcName+"Validator")
dynamicDomainService.updateSessionFactory(gac)

}

if(isDC){
def session = sessionFactory.getCurrentSession()
def connection = session.connection()
def state = connection.createStatement()
state.addBatch("drop table ${this.name.toLowerCase()}")
state.executeBatch()
}

But still :( table gets created when we reload any other updated domain class.

Thanks,
Main Pal
Reply all
Reply to author
Forward
0 new messages