tags:

views:

33

answers:

2

Hi,

I have an sql query in my controller action (select * from table....where.....et al). This query is fired when the user submits a page.

It is a 50 lines code that takes in 3 parameters. For eg: select * from employee where empDate='params.empDt' and empNum=params.empNum order by params.sort asc.

In the above case the query takes in 3 parameters (params.empDt, params.empNum and params.sort) dynamically and is executed. Since it is a huge query, I am looking at externalising the query to an sql file. The file thus externalised would be called in the service and the query would be executed.

So i created the .sql file in grails-app/conf/sql/read_date.sql.

When i read this file and the run the app, using run-app, it wrks fine. I am able to read the file and execute the query.

However, when I create a war and deploy it on tomcat, the appn doesnt read the file and I get a FileNotFound Exception.

java.io.FileNotFoundException: grails-app/conf/sql/read_date.sql (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:106)

Inputs??

A: 

Well you see the thing is, the grails-app/conf structure does not exist within the war. If you use something like 7-zip to open up the war and take a look around in it, you will see that all the Classes in conf are now in Web-Inf/classes. The directory structure is only really guaranteed through development. If you created an Sql file in web-app/sql, this might solve the problem for you as the directories under web-app are respected during War generation. Web-app contains thing like javascript files that are required by the application and are then accessible via http://myhost:port/AppName/sql/my.sql. If I were you, I would database the sql though.

Hope this helps. John

John Rellis
Thanks John.I did the foll. I created the sql file under web-app/sql/read_date.sql. This too works on my dev set up.I have a constants file that mentions the file path which has the below code. public static final String READ_DATE_FILE = "sql/read_date.sql"I read this from my service class as String readDate= new File(EmployeeConstants.READ_DATE_FILE).textWhen I create the war and deploy it on tomcat, I again get the foll error."java.io.FileNotFoundException: web-app/sql/read_date.sql (No such file or directory)"Am i mentioning the file path in the wrong way?Please advice.
Bob
Hey,when you are deployed in Tomcat you cannot access items in the war like they are on the file system, you can however access them via a URL if they exist in web-app but so can anyone else if you are not careful so I am not sure how safe this is. Check here to read URL contents, http://mrhaki.blogspot.com/2009/10/groovy-goodness-reading-url-content.html , either read from the URL or place the files on your file system outside of the war and put a config option in Config.groovy to allow you to choose a path.
John Rellis
A: 

I can't really see the point of putting the actual SQL in an external file. Just put the SQL statements in the service class, things will be fine. There is probably no increase in speed if you have the SQL statements in an external file (that needs to be read from disk every time the query needs to be executed). If you need to change the SQL Statement often (which would be a good reason to externalise the statement), you could evaluate the possibility of reading it from the database itself (like from a text field, or something)...

If you really want to use the external text file approach from a service or controller the easiest method is probably to use ServletContext.getResource to get a reference to the data packaged in your web app. Since getResource does take a context-relative URL, you can use getResource("/someSQLstatements.txt") and trust it to work, regardless of the location of your web application on the server's local filesystem OR the path it's mapped to via the servlet container. That should work well. (see also :http://www.velocityreviews.com/forums/t131134-specifying-path-for-file-to-be-read-by-servlet-with-tomcat.html)

have fun.

Sebastian Hilder
Thanks Sebastian....This worked...Thanks so much for the help
Bob