2 Replies Latest reply on Oct 22, 2006 7:10 AM by digitalgm

    Proper code?

      All, I am working on making a facade for some java functions that need to be used. I want to make doubly sure that there is no chance of memory leaks using this type of code. As you can tell I am using a facade to interface with the java component but also add methods to it. Please please let me know if this code is safe.

      <cfcomponent name="TestComponent" output="false">

      <cffunction name="init" returntype="any">


      variables.objCCC = createObject("java","com.ablecommerce.ac5.product.Category");

      return this;


      <cffunction name="load">

      <cfargument name="token" type="any">
      <cfargument name="categoryid" type="numeric">


      return objCCC.load(arguments.token, javacast("long",arguments.categoryid));


        • 1. Re: Proper code?
          MikerRoo Level 1
          A few potential gotchas, other than that, it's OK....
          1. "Name" is an invalid attribute! Perhaps you meant displayName?
          2. Be careful with the "variables" scope in components. Declare these outside the cffunction if they are meant for the whole object.
            Use "this" scope for anything meant to be public (not usually a good idea).
            Use the "var" qualifier for any variables declared inside functions.
          3. If that java uses a shared resource or spawns asynchronous threads, there might be trouble. Use locking if needed and monitor thread state if applicable.

          • 2. Re: Proper code?
            digitalgm Level 1
            Thank you very much for your answer.

            1. Yes, I did know that the name is not supported - I didnt cut and paste this, I actually typed it here so I added that by mistake

            2. Id like to make it so whenever the CFC is constructed (init function), I make the Java object. This way, in later functions of the CFC, I am able to use methods of the java object. Is there any harm in using the variables scope for where I have the object sitting right now. As I understood it, the variables scope exists only as long as the object exists.

            3. This is what I am the most concerned about. I was thinking about using jvmstat as well as a load simulator to put a bunch of load on the server and then monitor how well the GC was taking place. Also, at a simpler level, I could monitor how much memory was used before and after the load simulator. For example, if the java process is taking 250MB of memory at idle and then I run a test that generates 5000 of these objects and then stops. I should see a sharp rise of the amount of memory taken but then it should go back to idle, right?

            Thank you for your very valuable input. I really appreciate it!