Archive for the ‘General Technology’ Category

Using PHP FastCGI mode on Windows Server 2003 x64 w/ IIS in 32-bit mode

I recently needed to upgrade the PHP for a particular site from 5.2.6 to 5.3.0.  Of course, this is when I found out that PHP no longer supports the ISAPI module and instead wants you to use FastCGI.  Luckily I found a great article on how to configure IIS at http://learn.iis.net/page.aspx/247/using-fastcgi-to-host-php-applications-on-iis-60/.  However, these instructions have some caveats in regards to running IIS in 32 bit mode on Windows Server 2003 x64.  To make a long story short, just like with most things running in 32 bit mode on x64, everything that references %WINDIR%\system32 really needs to point to %WINDIR%\SysWOW64.  If you use the fcgiconfig.js script, this will configure everything to point to %WINDIR%\system32.

  • The Web Service Extension needs to point to %WINDIR%\SysWOW64\inetsrv\fcgiext.dll.
  • The .php extension on the website also needs to point to %WINDIR%\SysWOW64\inetsrv\fcgiext.dll.
  • The fcgiext.ini file must also be located in %WINDIR%\SysWOW64\inetsrv.  Otherwise you will get a 500 error from FastCGI saying it cannot find the configuration file.

Note that the fcgiconfig.js script is only installed in %WINDIR%\system32\inetsrv.  I was able to create a fcgiconfigwow64.js file that points to the correct files.  The script needs to have at least an ini file with the “[Types]” section in the %WINDIR%\SysWOW64\inetsrv directory.  It also needs to be located under %WINDIR%\system32 as it doesn’t support running under Wow64.  You just need to change the following 2 lines near the top of the script.

From:

var g_iniPath       = g_Shell.ExpandEnvironmentStrings( "%WinDir%\\system32\\inetsrv\\fcgiext.ini" );
var g_extensionPath = g_Shell.ExpandEnvironmentStrings( "%WinDir%\\system32\\inetsrv\\fcgiext.dll" );

To:

var g_iniPath       = g_Shell.ExpandEnvironmentStrings( "%WinDir%\\SysWOW64\\inetsrv\\fcgiext.ini" );
var g_extensionPath = g_Shell.ExpandEnvironmentStrings( "%WinDir%\\SysWOW64\\inetsrv\\fcgiext.dll" );

Unidev Employees Attend St. Louis Day of .NET Conference

The second annual St. Louis Day of .NET conference was held on August 28th and 29th, 2009. Over 500 local .NET developers, architects and other technologists, including Unidev’s George Zheng and Jinhai Wang, attended the highly anticipated event at the exciting location of The Ameristar Casino in St. Charles, Missouri. The conference, with too much info to pack into just one day, had over 50 technical sessions from local and national experts on Saturday and Sunday covering all the technologies of today, as well as providing a sneak preview of the technology possibilities of the future.

George and Jinhai took a lot of useful information from the conference and were exposed to new skills and ideas. “Day to day we use the various Microsoft Tools to focus on one project, but the conference covered every aspect of the tool. Meeting with other users of the same tools, helped me to become quicker, more educated and more productive,” George Zheng says of his experience at the conference.

Jinhai found the session on Language Integrated Query (LINQ), presented by Keith Dahlby, Senior Consultant at Inetium, to be most impressive. The session explored technologies that make LINQ possible and discussed how you can use the same techniques to make LINQ work for you. Another session Jinhai and George both found notable was the session on Silverlight presented by Brad Tutterow, Senior Consultant at Daugherty Business Solutions. Silverlight is a browser plug-in that allows .NET developers to write rich internet applications (RIAs). The session covered how to use Visual Studio and the Expression Studio suite together to build a working Silverlight game, the outcome of the presentation was a working Silverlight game that participants got to take home with them.

A few of the session topics covered at the conference included C #, VB.NET, Sharepoint, WPF, WCF, Visual Studio, .NET 4.0, Expression Blend, SQL Server 2008, ASP.NET MVC and Windows Azure. In an effort to provide the most rewarding experience possible to participants, this year’s conference offered a wide variety of alternative sessions and other unique opportunities such as roundtable discussions, “Birds of a Feather” opportunities, a vendor fair and a Friday night social at Home Night Club, located in the Ameristar Casino.

George and Jinhai both saw the .NET Conference as a success and found the experience to be extremely beneficial to their work at Unidev. They are looking forward to what the St. Louis Day of .NET 2010 Conference has in store!

Bidirectional Associations and JiBX

I recently needed to use JiBX to parse a XML document into domain objects that contained bidirectional associations with collections.  The issue started out as a simple NullPointerException since each child object did not have a reference back to its parent. 

After some searching on Google, the only suggestions that I came across had to do with using the post-set attribute, which gets called after the class has been unmarshalled.  For various reasons, most of which I did not want the domain classes tied to JiBX, I sought out a different solution.

The solution I came up with was to use a set-method for the collection instead of field.  So I replaced field=”fieldName” with set-method=”setFieldName”.  This set method ensures that the bidirectional association is properly defined. 

There is an argument to be made that I should be doing this anyway.  After all, the domain class probably should not let it be up to the caller to ensure the bidirectional association is properly defined, regardless if it is being called from JiBX or some other caller.

However there was one unintentional side affect.  The project is also using Hibernate.  The same domain classes were mapped using lazy associations.  This change to the set method in affect turned off the laziness.  The solution for this was to change the Hibernate mappings to use field access to bypass the set method.

Paging the query result

If you have 5000 products in database, you may like to show them in pages on the screen. It’s a common request to only retrieve the records on a certain page. This query must be quick and efficient if we are talking about millions of records in a table for an AJAX control.

In Oracle, we can do this:

SELECT * from
(
SELECT *, ROWNUM as rownumber from Product order by name
)
where rownumber between (PageNbr - 1)*PageSize + 1 and PageNbr*PageSize

As you can see, this query needs to fetch all rows first then narrows down the result. A more efficient query is:

select *
from ( select a.*, rownum rnum
from ( select * from product order by name) a
where rownum <= PageNbr*PageSize)
where rnum >= (PageNbr - 1)*PageSize + 1

Oracle will optimize this query, so it is not as resource intensive as the first one.

In SQL Server 2005, we can do similar query:

WITH Temp AS
(
SELECT row_number() OVER (ORDER BY [name]) AS rowNum, * from Product
)
SELECT * FROM Temp
WHERE rowNum between (@PageNbr - 1) * @PageSize + 1 and @PageNbr * @PageSize
ORDER BY [name]

The query had been optimized in SQL Server as well.

Google App Engine is open for Java Now

 Google App Engine Java Overview.

You can sign up here.

XMarks Smarter Search feature on IE8 produces mixed content warning

I just installed XMarks to see if I can start synchronizing my bookmarks between all of my various locations and all the browsers installed.  However soon after installing, I noticed that I would get the mixed content warning (the one telling you are downloading unsecure content from a secure page) every time I would go to our company portal page, which is always secured via SSL.

I double checked the sites using Firefox, and even IE 7 from another machine, and the warning did not appear.  I disabled the Smarter Search feature in XMarks and the warning went away.

I have only installed XMarks on IE 8 so I don’t know if it is specific to IE 8 or is a problem with other browsers in general.

Update:  As per Colin below this issue has been fixed with version 1.0.7.  I installed this version and the warning no longer appears.  It is refreshing to have such a quick response.

The Four Pillars of ASP.NET

Paul Litwin posted an interesting article that discusses the four pillars of ASP.NET (Web Forms, MVC, AJAX, and Dynamic Data).

He said: “Microsoft used to present ASP.NET Web Forms vs. MVC as a choice between a car and a motorcycle. Both will get you to your job , but some (the majority of the population, I might add) prefer driving a car, while a sizable minority love their motorcycles which give you better gas mileage and independence, but don’t protect you in the rain. To stretch this analogy to its breaking point, let me suggest that ASP.NET AJAX is like riding a bicycle to work (lean and mean, best gas mileage, but it requires you to exercise for your commute and exposes you to the elements like the motorcycle) while Dynamic Data is like taking the bus to work (let metro do the driving for you.)”

You can read his blog entry here:

Calling Oracle Stored Procedure from Hibernate

While calling Oracle stored procedure from Hibernate  there is couple of rules that we need to follow. There can be only one return value and this must be a reference cursor. This return reference cusor must be the first and only out value for the stored procedure. It is required that stored procedure must return a reference cursor to be used from Hibernate.

Here is my stored procedure which return book name and ISBN number for a given branch and for a given author

CREATE OR REPLACE PROCEDURE SP_LIB_DTL(p_cursor    out sys_refcursor,
                                       in_brnch_cd in number,
                                       in_auth_cd in number)
as
  bookName varchar2(8);
  ISBN     number;
begin
  bookName := null;
  ISBN     := 0;
  open p_cursor for
    select l.book_name, l.isbn_nbr
      into bookName, ISBN
      from LIB_BRNCH_DTL l
     where l.branch_code = in_brnch_cd
     and l.auth_code = in_auth_cd;

end;

HIbernate Mapping for return class and Named Query:

<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“>
<hibernate-mapping>
<class name=”com.org.lib.LibraryDetails”>
<id name=”ISBN” type=”long” />
<property name=”bookName” type=”string” />
</class>
 <sql-query name=”LIB_SP” callable=”true”>
 <return class=”com.org.lib.LibraryDetails”>
   <return-property name=”ISBN” column=”isbn_nbr” />
   <return-property name=”bookName” column=”book_name” />
 </return>
  {  call SP_LIB_DTL(? , :branchCD ,:authorCD) }
 </sql-query>
</hibernate-mapping>

Make sure you have used the correct database field name value for the column attribute for the return property mapping. You will get the following error if you don’t map the correct databse field name

could not execute query; bad SQL grammar [{  call SP_LIB_DTL(? , ?) }];
nested exception is java.sql.SQLException: Invalid column name

Here is the DAO implementation for executing query and to set the bind parameter values.

public  List selectBooks(final BigDecimal branchCode,final BigDecimal authorCode){
        return (List) getHibernateTemplate().execute(new HibernateCallback() {
              public Object doInHibernate(Session session) throws HibernateException, SQLException
              {
                  Query q = session.getNamedQuery(“LIB_SP”);
                  q.setLong(“branchCD”, branchCode.longValue());
                  q.setLong(“authorCD”, authorCode.longValue());
                  return q.list();
              }
          });
      }

VS2008 Tips & Tricks

As a .NET developer, I’m sure you have your own tricks to make your life easy. But you may still like to know more tricks on Visual Studio. Read this article to see if you know all of them…

Essential Visual Studio Tips & Tricks that Every Developer Should Know

Math Error in JavaScript

Believe it or not, if you do this in JavaScript: 103 x 67.12, you will get 6913.360000000001. This happens on both Microsoft Internet Explorer and Firefox. Try this your self.


Basic Math Errors in JavaScript

The calculation… Should equal… Your browser’s
  answer…
81.66 * 15 1224.9
103 * 67.12 6913.36
24.88 + 4.35 29.23

In terms of mathematical accuracy, 81.66 times 15 unequivocally equals 1224.9, as any sixth grader with a pencil and a piece of paper can demonstrate. But computer is in binary world. it has to convert decimal to binary number before calculation and there may be a problem to convert floating point decimal to binary. This is same for all computer languages. To avoid this, you need to know the scale (the number of decimal positions). Then you can convert the floating point decimal to an integer and scale it back at the end. i.e. (67.12 * 100) * 103 / 100. In Java, BigDecimal can be used to setup the scale.

JavaScript has a built-in method – toFixed, can be used to fix this issue easily as well.

amount = 103 * 67.12;
result = amount.toFixed(2);

So don’t forget this when you deal with a floating point decimal even for addition or multiplication.