Archive

Archive for the ‘Programming’ Category

File upload not working with spring roo (java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream)

June 6, 2011 Leave a comment

I have published spring roo project (spring mvc) on tomcat 7. Everything worked fine, except file uploading.

After submitting page with selected file, I’m getting java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream (full stack trace is below).

Google showed me that I was missing commons-io.something.jar library. I’ve copied org.apache.commons.io_2.0.1.v201101200200.jar to the WEB-INF/lib directory, restarted tomcat server, and it’s working fine.

Full stack trace:

SEVERE: Servlet.service() for servlet [RANDB] in context with path [/RANDB] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream] with root cause
java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
        at org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196)
        at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358)
        at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
        at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:155)
        at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:138)
        at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:887)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:750)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
        at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
        at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:636)
Advertisements

Linq to sql, ctx.submitchanges progress

January 2, 2011 1 comment

Linq to sql provides no default mechanism for tracking progress of the ctx.submitchanges() method.

This can be troublesome if you are using batch import with linq to sql (which is not the best practice by itself).

I asked question on the stackoverflow
http://stackoverflow.com/questions/4563828/linq-to-sql-submitchangess-progress

Response to the similar question was to divide inserts into multiple ctx.submitchanges() methods. This would work fine, but I needed transactions (all inserts or none)

I can get total number of inserts by using ctx.GetChangeSet().Inserts.Count;

Using ActionTextWriter class I found on
http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers

I was able to track progress of the inserts using following snippet of code

ctx.Log = new ActionTextWriter(s => {
  if (s.StartsWith("INSERT INTO"))
    ReportProgress();
});

Linq to sql will write to the log file every sql statement. if the statements begins with INSERT INTO, that’s new insert.

Evertything I need for the progress bar is here 🙂

Also, performance wise, I’m reporting progress for every 10 000 inserted records. Refreshing screen too often can be slow sometimes.

LinqToSql Sum over empty collection causes exception

October 15, 2010 Leave a comment

Linq to Sql Sum method over empty collection returns null. Linq to object Sum method over empty collection returns 0.

This Linq To Sql behavior can cause exception :

"The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type."

This will happen if you try to assign sum to decimal. Decimal is not nullable. If sum is called over empty collection (e.g. no items are return from where clause) sum will try to return null. decimal is not nullable and you have exception Sad smile

One solution is to cast sum so it returns nullable decimal (decimal ?), Second solution is to use linq to object, because, linq to object sum over empty collection returns 0.

Why is all this happening?

In SQL, Sum() aggregate operator returns null for an empty set. So this is as designed.

Since users often rely on LINQ to SQL producing the same results as an equivalent SQL query, we decided not to convert the null to zero as in case of LINQ to Objects. There are a number of cases where we considered forcing .NET/C#/VB semantics for consistency with LINQ to Objects vs retaining SQL semantics for consistency with SQL. In most cases, we favored the latter for consistency and more enforceable and performant implementation,

Source: http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/479f2823-c942-4990-adca-21e03f969a12

 

 

You can read more about this issue on

http://weblogs.asp.net/zeeshanhirani/archive/2008/07/15/applying-aggregates-to-empty-collections-causes-exception-in-linq-to-sql.aspx

and

http://stackoverflow.com/questions/696431/linq-query-with-nullable-sum-problem/