Wednesday, December 11, 2013

Windows task manager

Having trouble figuring out which process is which in taskManager

Obviously there is also sysInternal ProcExp, however this isn't always available if you are troubleshooting a Production issue

heres some tips
wmic process list
or for more specifric fields you can use Get
WMIC PROCESS get Caption,Commandline,Processid
http://stackoverflow.com/a/12850419/249672

If you want to get more information from a process (such as command line used to start it), then you can combine this with TaskMgr. (First add processId to the list of attributes that TaskMgr displays), then correlate it with ist from wmic
e.g. if grep installed on windows
wmic process get commandLine,processId | grep
if grep is not instaled (there seems to be a problem with findStr), you can pipe it to a temp file and use a decent editor such as notepad++
wmic process list > tempFile

Also tasklist is a command line version of taskMgr. It lists PID, and memory usage, so if you can't add PID to the taskManager (apparenty can't in windows server 2003), then you can correlate the memoryUsage back to taskManager and get the PID that way. (Note taskList has some other options  that may help)


Wednesday, November 27, 2013

groovy cusips

Some sample code for verifying cusips


// Note must map these character specifically since ascii mapping does not work
//println "*#@ = "+((int)('*'-'A'))+", "+((int)('#'-'A'))+", "+((int)('@'-'A'))

// sample cusips taken from http://www.ksvali.com/2009/02/security-ids-symbol-cusip-isin-sedol-ric-code/
def cusips = [  "14149YAR9", // - Corporate Bond - Cardinal Health Inc
                "126650BG4", // - Corporate Bond -CVS Caremark Corp
                "254709AC2", // - Corporate Bond -Discover Finl Services
                "437076AQ5", // - Corporate Bond -Home Depot Inc
                "441060AG5", // - Corporate Bond -Hospira Inc
                "50075NAN4", // - Corporate Bond -Kraft Foods Inc
                "574599BE5", // - Corporate Bond -Masco Corp
                "617446B99", // - Corporate Bond -Morgan Stanley
                "637640AC7", // - Corporate Bond -Natl Semicon Corp
                "713291AL6", // - Corporate Bond -Pepco Hldgs Inc
                "852061AE0", // - Corporate Bond -Sprint Nextel Corp
                "887317AA3", // - Corporate Bond -Time Warner Inc
                "925524BF6", // - Corporate Bond -Viacom
                "125509BG3", // - Corporate Bond -Cigna Corp
                "125896AV2"] // - Corporate Bond -CMS Engy Corp

cusips.each(){
    println "Verifying $it "+verifyCusip(it)   
}

public boolean verifyCusip(cusip){
   int check = getCheckCode(cusip.trim().toUpperCase())
   //println "  CheckCode = "+check)
   //println "Checking $check == "+cusip.charAt(cusip.size()-1)
   return check==((int)cusip.charAt(cusip.size()-1)-(int)'0')
}

/**
 *Code based on algorithm at https://en.wikipedia.org/wiki/CUSIP
 */
public int getCheckCode(String cusip){
   int sum = 0
   int v,p
   for(i=0;i<8;i++){
      char c = cusip.charAt(i)
      if(Character.isDigit(c))
         v = (int)c-(int)'0'
      else if(Character.isLetter(c))
         v = (int)c - (int)'A'+10
      else if(c == '*')
         v = 36
      else if(c == '@')
         v = 37
      else if(c == '#')
         v = 38
      if(i%2!=0) // if i NOT even   N.B. since we count from 0 it is not even.. If we count from 1 it is even
         v *= 2
      //println "  for letter $c val = $v"
      sum += v/10 + (v %10)
   }
  
   return (10 - (sum % 10)) % 10
}

Monday, November 04, 2013

7 zip

7zip is a nice free ware zip program.

For some reason my install had failed to get it opening .zip files by default. (Despite setting it multiple times via the 7zip options page.)

Heres how I fixed it (thanks winzip).. http://kb.winzip.com/kb/entry/155/

Also worth noting, is that it is good at deleting files and directories that other programs (including windows explorer) can't. I have had to install it, in order to delte some stubborn files that were lying around.

Thursday, October 10, 2013

Groovy Closures, and line endings

I've been using Groovy for a while now so when I was coding the following and it was crapping out at the compiler stage I was confused

Date newDate =  (Date)toObject("Problem converting imDate", new StringBuilder(), [:], {
            println "Parse date = $it";
            df.parse(it.date) }
)
private Object toObject(def errMsg, StringBuilder errors, def row, Closure c){
        try{
            Object ret = c.call(row);
            if(!ret)
                throw new RuntimeException("Returned null")
        }catch (Exception e){
            errors.append("$errMsg :-${e.getMessage()}\n")
            null
        }
    }


This is simply creating a method with a Closure parameter and calling it with an anonymous closure. Simple stuff.. But the compiler doesn't like it.

BTW I did look up using Types and Generics so I didn't need to cast the response, but looking into it it doesn't appear to be possible to define the return type of a Closure.. so little bit of a gap there in groovy.

Changing the closure from anonymous to defined fixed the problem
def c= {
            println "Parse date = $it";
            df.parse(it.date) }

Date newDate =  (Date)toObject("Problem converting imDate", new StringBuilder(), [:],c)

But I didn't want to do this, as I was calling the method multiple times and it messed up my Feng Shui.

So the problem, apparently was the casting. It seems to expect the object to cast to appear on the same line as the cast (Date) directive. Wrapping the method call in  parenthesis  fixed the problem.

Date newDate =  (Date)(toObject("Problem converting imDate", new StringBuilder(), [:], {
            println "Parse date = $it";
            df.parse(it.date) }
))


Monday, September 23, 2013

Big Numbers

Sometimes it good to have them all listed in one place

1000's10'ssymbolprefixname
100010^3kkilothousand
1000^210^6Mmegamillion
1000^310^9GgigaBillion
1000^410^12TteraTrillion
1000^510^15PpetaQuadrillion
1000^610^18EexaQuintillion
1000^710^21ZzettaSextillion
1000^810^24YyottaSeptillion

Sources

http://en.wikipedia.org/wiki/Names_of_large_numbers http://en.wikipedia.org/wiki/Binary_prefix

Monday, August 12, 2013

Grails command update

Grails is quite memory hungry. To save time I often update the  grails batch file to give more memory on startup, so you don't get those outOfMemory errors, such as
java.lang.OutOfMemoryError: PermGen space

%GRAILS_HOME%\bin\startGrails.bat 

to include the following line  (near bottem , e.g. on line 135 of grails1.3.5 startGrails.bat)

set JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx512m -XX:MaxPermSize=512m

Monday, July 22, 2013

SPA... Single Page Applications.. My journey


Going to try and document my forays into web 2.0 (old term but I think it makes sense for these new JS framework based webapps).

Heres my requirements. I want to develop a modern webapp using NodeJs on the server side, and some of the newer client side frameworks. As a tester project I'm going to implement a time management system. This is a personal bugbear of mine since every week, and month I have to struggle through some overwieght and cumbersome time managment tools to submit my work hours.

TO start with I'm going to try angularJS, and bootstrap.

So first step is find a good template/ boilerplate for this. I stumbled upon Yeoman (http://yeoman.io/) which is an opinionated (I like this) framework for front end developemnt.. (Its made up of 3 tools, yo (scaffolding cmd line tool), Bower (dependency mgmt), and grunt (build tasks such as minification etc), SO I'm going to start using that. (See Aside 1)

I already had nodejs installed, so here goes

npm install -g yo
npm install -g generator-webapp
npm install -g generator-angular
yo
yo angular --minsafe    

This gives you lots of options bu by default will setp angular, twitter, compass, and a load of angular addons

yo angular:controller myController
yo angular:directive myDirective
yo angular:filter myFilter
yo angular:service myService

Editors

In the past I've stuck to my Java IDE such as Eclipse or IntelliJ (see aside 2 below).
However nowadays all the kool kids are using Sublime.
Alternatives are Brackets and Atom (the one I'm using). Atom comes the apm (atom package manager) which is build on npm and comes with a rich array of plugins
Ones I recommend are
Typescript - since I am planning on using Typescript for my Js development going forward






Aside:
yo
Woah.. An old fashioned text based generator tool (and Paul Irish is one of the people behind it).. Old-skool I like it.. reminds me of rogue
AS you run npn install scripts, yo's menu will grow offering you new options (e.g. Run the Angular generator and webapp generator are added as you run the install scripts above).


Aside 1 Console2
One of the side benfits of going through this process was discovering a recommendation to use console2 (or powershell) for windows users. Heres a blog entryon getting the most out of it. http://www.hanselman.com/blog/Console2ABetterWindowsCommandPrompt.aspx

Aside 2: IntelliJ has a nodejs plugin http://www.jetbrains.com/idea/webhelp/browse-repositories-dialog.html#search
THis allows you to run nodejs from intellij. Need to point at app.js which i normally located at app\scripts\app.js

Rogue Aside
http://en.wikipedia.org/wiki/Rogue_(video_game)
Original ascii based maze dngeon game with randomly generated levels. (www.play.vg/games/87-Rogue.html?). Personally I found rogue way too difficult..
Nethack is another, again very difficult, but you do stsrt with a pet, and you can encounter your own previously killed characters in later games http://en.wikipedia.org/wiki/NetHack
Far more approachable and the only on eI managed to finish was Larn http://en.wikipedia.org/wiki/Larn_%28video_game%29

Wednesday, July 03, 2013

Groovy Dynamic code

This is fairly old, but still cool, and I just had a need to use it today, so I thought I'd post about it.

My problem was I wanted to mock out a Domain class. If a proerty value was null., then I wanted to create a mock value for that property. IF the property had a value, then I wanted to return that.

Using Dynamic programming this was easy.

class A{
  def doit(){
        println "doing..";
    }
    String var="yoyo";
}

def a = new A();

a.metaClass.getProperty = {p ->
    def meta = a.metaClass.getMetaProperty(p)
    if(meta){
        def mp = meta.getProperty(delegate)
        if(mp)
            return mp
    }
    return "DynoGet${p}"
}

println a.var
println a.mar
a.var=null
println a.var


Output

 
yoyo
DynoGetmar
DynoGetvar

The key here is the metaClass.getMetaProperty, and the meta.getProperty(delegate). They check if the property exists, first, and then if it has a value assigned. If not then I create a default value.



Monday, March 04, 2013

Gradle

Gradle is my new build tool favourite. I love the groovy syntax, and built in libraries, and find it a huge improvements on maven and ant.

gradle tasks   // This will list all tasks that build file supports
To exclude a task -x

e.g. to exclude tests from a build

gradle build -x test 

Gradle comes with a series of plugins which can be used for commonly executed builds, e.g. java, groovy, also ones for IDE's such as idea and eclipse. These provide tasks commonly used with these.

e.g.
apply plugin: 'java'   // Provides build compile test testCompile
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'groovy'
apply plugin:'application'



Running gradle tasks on this gives
C:\svn>gradle tasks
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Application tasks
-----------------
distTar - Bundles the project as a JVM application with libs and OS specific scripts.
distZip - Bundles the project as a JVM application with libs and OS specific scripts.
installApp - Installs the project as a JVM application along with libs and OS specific scripts.
run - Runs this project as a JVM application

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles the main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles the test classes.

Documentation tasks
-------------------
groovydoc - Generates Groovydoc API documentation for the main source code.
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
dependencies - Displays all dependencies declared in root project 'myproj'.
dependencyInsight - Displays the insight into a specific dependency in root project 'myproj'.
help - Displays a help message
projects - Displays the sub-projects of root project 'colline'.
properties - Displays the properties of root project 'colline'.
tasks - Displays the tasks runnable from root project 'colline' (some of the displayed tasks may belong to subprojects).

IDE tasks
---------
cleanEclipse - Cleans all Eclipse files.
cleanIdea - Cleans IDEA project files (IML, IPR)
eclipse - Generates all Eclipse files.
idea - Generates IDEA project files (IML, IPR, IWS)

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Other tasks
-----------
cleanIdeaWorkspace
dist
wrapper

Rules
-----
Pattern: build: Assembles the artifacts of a configuration.
Pattern: upload: Assembles and uploads the artifacts belongin
g to a configuration.
Pattern: clean: Cleans the output files of a task.

To see all tasks and more detail, run with --all.

BUILD SUCCESSFUL

Monday, January 28, 2013

Windows remote control

I'm going to try and gather some useful commands and tools here for controlling servers. Initially this is based ona  need to control a windows server, bu tI may add unix commands too just for completness.

Start Stop services
  • sc [command]
 One issue I had was permissions. Some service (run as local system user) were unable to start/ stop services on a different server (return code 5: Access Denied was returned)

SysInternals
To counter this you can use the following PsExec - http://technet.microsoft.com/en-us/sysinternals/bb897553    (taken from  http://serverfault.com/questions/359010/execute-windows-sc-command-as-another-user)

If you want to script using the sysInteranl files then the popUp EULA can cause problems. (It pops up and you have to accept it). One way around this is to add -accepteula to your script

e.g.
pskill -accepteula


Remote Desktop controls
List existing Remote Desktop connections. (Note in the end I ran these on the remote machine in question (after using the mstsc command to login below)
  • qwinsta /SERVER:
once you have the list, you can evict some users using
  •  rwinsta /SERVER:
e.g.
dir> qwinsta
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
>rdp-tcp#29        myUser                  0  Active  rdpwd
 rdp-tcp                                 65536  Listen  rdpwd
                   sa507823                  1  Disc    rdpwd
 console                                     4  Conn    wdcon


Here I had a disconnected instance still hanging around. This could be killed using
rwinsta 1

Also to 'force' a login you remoteDesktop command in console mode e.g.

You can run the remote desktop in consoel mode (http://h0w2.blogspot.com/2011/04/how-to-force-go-in-to-remote-desktop.html )
If you need to force, follow this trick :
- From the Start menu  click RUN Command
- type mstsc /console /v:nameserver or ip address of remote computer 
for example:   mstsc /console /v:192.168.0.10 
Or 
- type mstsc /console /v:nameserver or ip address of remote computer /admin
for example:   mstsc /console /v:192.168.0.10 /admin
- Click OK


Performance Monitoring

One of my goals for the next few months is to start colelcting system data from a number of computers, and run some data mining techniques to capture performance statistics, and monitor behaviour, and downtimes.

To this end the first step is to collect system data.

Windows
For windows machines I'm am trying out perfmon.msc. (See http://support.microsoft.com/kb/305610) It captures various metrics and can output them to a file. I can then periodically batch the file to a central area for processing

Unix
There are lots of various tools for unix varients. See http://www.informit.com/articles/article.aspx?p=29666&seqNum=4
  • top
  • vmstat
  • ps aux
  • bonnie (for disk monitoring)
from here http://www.unix.com/linux/24390-linux-unix-performance-monitoring-2.html

Unix Performance Montioring commands
-------------------------------------------------------------->
vmstat - report virtual memory statistics
iostat - report io statistics
mpstat - report processor statistics
netstat - report network statists
prstat - process statistics

Thursday, January 10, 2013

VisualVM notes and tcServer


Over the years I have had to use visualVM every one in a while in order to help monitor a running application, or help isolate some problem, or generate a thread dump (eg. for a remote running application server)

Heres some notes on my experiences.

1/ Where is it.
VisualVM is located in your JVM (1.6 rel 7 and above).. Note filename is jvisualvm in the jdk/bin fir, e.g. $JAVA_HOME/bin/jvisualvm

2/ When running locally it shoudl immediately list all jvms running.
One case where I didn't see this happening was when testing a tcServer instance. This was launched via the wrapper.exe prog (to turn it into a service).

In orer to conenct to these you simply have to point at the JMX port.
This can  be found in the server.xml in the conf folder on tcServer. There should be a configuration for a listener e.g.
              authenticate="true"
              bind="127.0.0.1"
              className="com.springsource.tcserver.serviceability.rmi.JmxSocketListener"
              passwordFile="${catalina.base}/conf/jmxremote.password"
              port="${base.jmx.port}"
              useSSL="false"/>


In my case the $base.jmx.port was specified in the catalina.propeties
base.jmx.port=21069

Conencting was simply a case of right clicking and entering localhost:21069 as the jmx connection url.

Login credentials are required. Again these are configured via the listener in the  server.xml above. In my case it was using the default values of admin/ springsource

Tuesday, August 14, 2012

Grails and MongoDb

These are a few notes I took while setting up a project using grails 2.1.0 and mongodb 2.0.6.

Firstly there is a grails plugin for mongoDB. The notes for its use are here

So you can run grails install-plugin mongoDB (or update the BuildConfig.groovy as is the prefered method for grails guru's).

If you want Mongo to be your main DB, then you need to uninstall the hibernate plugin. This shoudl be done via the BuildConfig.groovy. (Comment out the //runtime ":hibernate:$grailsVersion" line)

Next you also need to remove the database-migration plugin, since this is uses the hibernate plugin (//runtime ":database-migration:1.1")

My mongoDB was running on port 27017 which is different from the post specified in the grails mongoDb tutorial (27107), so this must be updated the Datasource.groovy

After a bit of searching I found this sample MongoDb data file . (See http://stackoverflow.com/questions/5723896/is-there-a-sample-mongodb-database-along-the-lines-of-world-for-mysql)

To create a grails domain to fit in with the zips.bson data I did the following. Note we want the id field to be mapped to the mongo _id field, and since it represents the zip code we need to mark the generator as assigned.

class Zip {
    String city
    List loc
    Integer pop
    String state
    String id
   
   
    static mapping = {
        id generator: 'assigned',
        column: '_id'
    }

    static constraints = {
    }
}




Thursday, June 21, 2012

Grails/ Hibernate clean schema

Sometimes you may want to clean up the entire DB. Grails does this for you if you specify create-drop as the datasource dbCreate property, but sometimes (e.g. if you are deploying to a test of prod server, you can't set this to create-drop), you might just want to be able to clean down the database.



One handy script to clear the DB is to run grails schema-export export. This will run the hibernate schema-export ant task, setting the flag to clear the DB. (It will recreate it again after, using the new schema).

If you want to permanently delete the schema you can modify the schema-export and create your own, e.g.

...
target(cleanDb: 'Clean Db using Run Hibernate SchemaExport') {
    depends(packageApp)

    configureFromArgs()

    def file = new File(filename)
    ant.mkdir(dir: file.parentFile)

    configClasspath()
    loadApp()

    populateProperties()

    def configuration = classLoader.loadClass(configClassName).newInstance()
    configuration.setGrailsApplication(grailsApp)
    configuration.setProperties(props)
    def hibernateCfgXml = eventsClassLoader.getResource('hibernate.cfg.xml')
    if (hibernateCfgXml) {
        configuration.configure(hibernateCfgXml)
    }

    def schemaExport = classLoader.loadClass('org.hibernate.tool.hbm2ddl.SchemaExport')
        .newInstance(configuration)
        .setHaltOnError(true)
        .setOutputFile(file.path)
        .setDelimiter(';')

    def action = "Cleaning Db "
    println "${action} in environment '${grailsEnv}' using properties ${props}"


    // 1st drop, warning exceptions
    //schemaExport.execute(stdout, true, true, false)
    schemaExport.drop(true, true)


    if (!schemaExport.exceptions.empty) {
        schemaExport.exceptions[0].printStackTrace()
    }
}




Eager fetching grails and Hibernate

This is taken from grails docs http://grails.org/doc/latest/guide/GORM.html#fetching  
 Basically for single associations use fetch: 'join' (using lazy:false means that there will be 2 sql statements per association.). However for one to many use lazy:false

Configuring Eager Fetching

An alternative approach that avoids the N+1 queries is to use eager fetching, which can be specified as follows:
class Airport {
    String name
    static hasMany = [flights: Flight]
    static mapping = {
        flights lazy: false
    }
}
In this case the flights association will be loaded at the same time as its Airport instance, although a second query will be executed to fetch the collection. You can also use fetch: 'join' instead of lazy: false , in which case GORM will only execute a single query to get the airports and their flights. This works well for single-ended associations, but you need to be careful with one-to-manys. Queries will work as you'd expect right up to the moment you add a limit to the number of results you want. At that point, you will likely end up with fewer results than you were expecting. The reason for this is quite technical but ultimately the problem arises from GORM using a left outer join.
So, the recommendation is currently to use fetch: 'join' for single-ended associations and lazy: false for one-to-manys.
Be careful how and where you use eager loading because you could load your entire database into memory with too many eager associations. You can find more information on the mapping options in the section on the ORM DSL.

Tuesday, June 12, 2012

Maven

Some quick maven tips.

To run some code from a maven project
See this link http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/

mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2"

Article explains 2 other ways from mvn pom.xml 

To run unit tests from a maven project
See this link http://maven.apache.org/plugins/maven-surefire-plugin/examples/single-test.html

mvn -Dtest=TestSquare,TestCi*le test
 
(Note can have wildcards in class names)
 
 
Can also specify methods (with wildcards)
mvn -Dtest=TestCircle#test* test
 
To not run tests
I used to use 
mvn -Dmaven.tests.skip install 
 
back when I used maven 2.x. 
However with maven 3, you can skip the entire test phase by this switch
 
mvn -DskipTests=true isntall
 
 

Thursday, June 07, 2012

SSH and Putty

Here's a little time saver for SSH and Putty.

1/ Putty (See http://www.howtoforge.com/ssh_key_based_logins_putty)
Use PuTTYgen to generate a new private/ public keypair
  1. Create a new putty connection
  2. Enter a hostname and a "Saved Sessions" name
    1. Under Connection->Data->Username enter 'username'
    2. Under Connection->Data->SSH->Auth->Private File key for Authentication enter the path of the Private Key
  3. Save the connection
  4. Click Open - You should be logged in without being prompted for user/pass combo.




2/SSH Key Setup for internode SSH
This allows you to ssh between machines without re-logging in
  1. Start a Putty session to each node you wish to reach
  2. Run "ssh-keygen -t rsa" on each one
  3. Append the contents of ~/.ssh/id_rsa.pub to the ~/.ssh/authorized_keys on each of the other machines
You should be able to ssh between each machine (any combo) now without a password.

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 java.security 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.

 bank.each(){
        def dcmAccounts = DcmAccount.findAllByDcm(it, [fetch:[fund:"eager", clearingHouse:"eager"]]) // This line fixes the problem
        Long subPercent =percent
        if(dcmAccounts.size()>0){
            subPercent = percent / dcmAccounts.size()
        }
       
        dcmAccounts.eachWithIndex {DcmAccount dcmAccount, cIndex ->
          //def exchangeRate =
ExchangeRate.findByCurrency(dcmAccount.fund.curerncy)?.rate  // 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(fundNumber:dcmAccount.fund.fundNumber,fund:dcmAccount.fund, dcmAccount:dcmAccount, limit: dcmAccount.excessCollateralLimit)
          fundSegregation.buildDcmCollateral()
          fundSegregation.buildPledgedCollateral()

Monday, April 02, 2012

JNDI with grails and TcServer

Just something to be aware of.

In order to reference JNDI resources from a grails (and probably java spring) app on TC Server there are 2 steps to perform.

1/ Add the jndi name to the tcserver conf/server.xml file. This will declare the jndi name and the resource that it represents. In your head you may assume that this is the only step you need (I know I did)...

This is a configuration object in the conf dir of your tcServer directory
e.g. (Note also have variables e.g. my.url defined in catalina.properties here)

<Resource accessToUnderlyingConnectionAllowed="false"
defaultAutoCommit="true" defaultReadOnly="false"
driverClassName="com.sybase.jdbc3.jdbc.SybDriver"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
initialSize="0" logAbandoned="false" maxActive="8"
maxIdle="8" maxOpenPreparedStatements="0" maxWait="-1"
minEvictableIdleTimeMillis="1800000" minIdle="0"
name="jdbc/myOtherDS" numTestsPerEvictionRun="3"
password="${my.password}" poolPreparedStatements="false"
removeAbandoned="false" removeAbandonedTimeout="300"
testOnBorrow="true" testOnReturn="false"
testWhileIdle="false" timeBetweenEvictionRunsMillis="-1"
type="javax.sql.DataSource" url="${myOther.url}"
username="${myOther.username}" validationQuery="select @@servername" />
<Resource accessToUnderlyingConnectionAllowed="false"
defaultAutoCommit="true" defaultReadOnly="false"
driverClassName="com.ibm.db2.jcc.DB2Driver"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
initialSize="0" logAbandoned="false" maxActive="8" maxIdle="8"
maxOpenPreparedStatements="0" maxWait="-1"
minEvictableIdleTimeMillis="1800000" minIdle="0"
name="jdbc/myDS" numTestsPerEvictionRun="3"
password="${my.password}" poolPreparedStatements="false"
removeAbandoned="false" removeAbandonedTimeout="300"
testOnBorrow="true" testOnReturn="false" testWhileIdle="false"
timeBetweenEvictionRunsMillis="-1"
type="javax.sql.DataSource" url="${my.url}"
username="${my.username}" validationQuery="SELECT 1 from sysibm.sysdummy1" />


2/ You also need to add the jndi name to META_INF/context.xml
This simply creates a mapping from the jndi name to a Java object (Normally a dataSource).

the context.xml is normally part of the source code for the application so will be checked into your source control repository.. You will need a new build if you add or change the data in here. This isn't a big deal really since you will need to add code to deal with the new or updated jndi object at any rate.

Sample

<Context path="myAppUrl" debug="1" reloadable="true" docBase="myAppDocBase" antiResourceLocking="true">
<ResourceLink global="jdbc/myDS" name="jdbc/myDS" type="javax.sql.Datasource"/>
<ResourceLink global="jdbc/myOtherDS" name="jdbc/myOtherDS" type="javax.sql.Datasource"/>
</Context>