Friday, May 25, 2012

Bouncy Castle PGP

I was testing some PGP methods from the Bouncy Castle API. When running their ClearSignedFileProcessor I got the following exception 
 java.lang.SecurityException: JCE cannot authenticate the provider BC

However steps 1 and 2 are not necessary
1. Find in /path_to_your_jvm/jre/lib/security
2. Add security.provider.9=org.bouncycastle.jce.provider.BouncyCastleProvider

if you instead explicitly add the Bouncy castle provider, (like is done in the example)
Security.addProvider(new BouncyCastleProvider());

Step 3 proved to be necessary. However, when I ran the program as part of a grails project... Strangely it wasnt' necessary. Not sure why that is

Friday, May 11, 2012

Grails org.hibernate.LazyInitializationException: could not initialize proxy - no Session

I was getting this exception which working on a series of domain objects at once. The problem is a well known one, and there are various posted solutions but none of them seemed to apply to me.

Stackoverflow.. They walk about using attach to manually attach to session..
Another Stackoverflow. This time talking about stale DB connections. Not applicable to me, but interesting nonetheless. 
Grails mailing list. Talks about wrapping DB calls in a withTransaction. This could be a runner.. I haven't tried it yet though.

 A quick explanation of my code. The code is calculating reconciliation values. Nothing complicated. The problem sems to be when I look up a currency value in a domain class embedded within the main loop domain object. I have loaded this domain object eagerly, so in theory all the component pieces should also get loaded, but this does not fix the problem.

The commented line  below caused the problems. Explicitly loading each component class seems to fix the problem. This is obviously slow , but speed is not important in this app.

One compounding problem was that this worked on my local environment, but failed in a test environment, so it proved difficult to recreate.

        def dcmAccounts = DcmAccount.findAllByDcm(it, [fetch:[fund:"eager", clearingHouse:"eager"]]) // This line fixes the problem
        Long subPercent =percent
            subPercent = percent / dcmAccounts.size()
        dcmAccounts.eachWithIndex {DcmAccount dcmAccount, cIndex ->
          //def exchangeRate =
ExchangeRate.findByCurrency(  // This line caused the problem. The fund object is lazily instantiated so when I go to access it, the session has expired. But eagerly loading the funds at the start fixed it
          def a=dcmAccount.fundId
          def b = Fund.get(a).currencyId
          def c = Currency.get(b)
          BigDecimal exchangeRate = ExchangeRate.findByCurrency(c)?.rate
          FundSegregation fundSegregation = new FundSegregation(,, dcmAccount:dcmAccount, limit: dcmAccount.excessCollateralLimit)