How to monitor your Oracle DB (Part 2)
Again, writting a little bit about Oracle administration.
Imagine the next situation: Database sessions are INACTIVE after a while and doesn't dissapear neither close, what can we do? We can use Oracle profiles. Basically, a profile is a set of limits that you can establish for resources in a schema. For our problem, we need to establish a profile that allow us to kill the INACTIVE sessions after some amount of time to clean the stuff.
To create and define profiles you need to have ALTER SYSTEM privileges. First of all you need to tell Oracle that you're able to establish limits to some system resources, to do that you have to modify RESOURCE_LIMIT parameter in the next way:
alter system set resource_limit=true;
Next, we have to create the profile and set it to specified schema. The syntax to do that is the next:(Continue reading)
How to monitor your Oracle DB (Part 1)
Sometimes, you are developing some application that connects to Oracle database and you need to monitor the database to know something about processes, open sessions/cursors and so on, so is necessary to know a little bit about Oracle administration.
Basically, Oracle provides us with a set of fixed tables that allow us to check database settings, processes, parameters, open sessions and so on that gives us important information. Some of these tables are the next:
V$SESSION /*Allows us to see open sessions and details about them*/ V$DATABASE /*Show us database information*/ V$FIXED_TABLE /*IMPORTANT because this shows a list of all fixed tables, learn that and you can get access all*/ V$LOCKED_OBJECT /*Shows us locked objects on the database*/ V$SQLAREA /*Show sql queries executed related to v$session*/
You can check a list of those views in the next link:
http://www.adp-gmbh.ch/ora/misc/dynamic_performance_views.html
Now, how to use that information to do different stuff on the database?
How to set ARITHABORT from JAVA – SQL Server
I had a problem yesterday. I was developing a RESTful web service where i have to run an UPDATE to SQL Server 2005 table, but when i do that i have the next problem:
UPDATE failed because the following SET options have incorrect settings: 'ARIHABORT'
Damn! What the hell is that? Googling i found this:
http://j2eeframeworks.blogspot.com/2008/10/insert-failed-because-following-set.html
And i understood that the table that i want to update is related to indexed views so the UPDATE or INSERT proccess isn't as simple because i had to set the parameter ARIHABORT = ON to do my operations.
So, how to do that?
How to use ModelDriven in Struts 2

Talking about Struts 2 again
. I wanna talk (or write) about this because is very interesting, for me it remembers me the way of work on Struts 1. For those that doesn't know what am i talking about, i'm going to explain you. In Struts 1, the way of work with Action and form properties is a little bit different of default Struts 2 way. In struts 1 we have the actions that doesn't contain any form property or getter/setter method, like this:
import....
public class LoginAction extends LookupDispatchAction {
public ActionForward logMeIn(HttpServletRequest request,
HttpServletResponse response,
ActionMapping mapping,
ActionForm form) {
LoginForm loginForm = (LoginForm) form;
if (loginForm.getUsername().equals("admin") &&
loginForm.getPassword().equals("admin")) {
return mapping.findForward("enter");
} else {
return mapping.findForward("fail");
}
}
}
And, in other class, the Action Form where we can find the properties with respective getter/setter methods and optionally, struts 1 server side validation:
But, in Struts 2 it changes a little bit because properties, getter/setter methods and validation are in the action. For the previous example, it's version on Struts 2 will look like this:
public class LoginAction {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public String logMeIn() {
if (getUsername().equals("admin") &&
getPassword().equals("admin")) {
return "enter";
} else {
return "fail";
}
}
Free from extends and strange return types in methods but more charged with properties and getter/setter methods, for some people it's better, for another people it sucks. In my case i like the fact that i don´t need to extend my actions from some classes (altought you have to do that if you want to inject additional funcionality to your action like validation, prepare scripts and so on) and a simpler way to declare action methods, but i dislike the fact that all is inside the action.
But don't worry people, Struts 1 developer that migrated to Struts 2 can still use the same model Action - Action Form like Struts 1 in struts 2
, how to do that?
How to avoid validation on Action call – Struts 2
As you know, like Struts 1, Struts 2 allow us to define server side validations when you call an action. This is very useful because sometimes javascript client side validation is not enought when we want to validate input entries against data from DB or do complex validations.
How to define validations in JAVA Struts 2? First your action must extend ActionSupport class, and override it's method validate that has te following signature:
public void validate() {}
Inside validate() you define your validations and, if you add messages to ActionErrors or FieldErrors, Struts 2 automatically detects that it must redirect to input result defined in action's declaration in struts.xml, like this:
<action name="login"> <interceptor-ref name="defaultStack" /> <result name="homePage">/login/login.jsp</result> <result name="input">/login/login.jsp</result> ... <result name="backToLoginPer">/login/login.jsp</result> </action>
The problem is that sometimes you want that validation applies only on certain action calls, that is, avoid validation on certain action calls. How to do this?
How to solve “cannot find javac” with JAVA SDK App Engine

Recently i decided to try Google App Engine, a service where you can host your applications built in different languages like JAVA, Ruby, Python and so on, using Google's infraestructure. Now is enough for me because i see that you can get this for free:
- 500MB storage
- Enough CPU and bandwidth to serve your applications to 5 million visits per month
So i want to test what kind of JAVA technologies it supports (I don't know, Struts2, Spring, Hibernate JPA) . The proccess to join this is very simple, the steps are the next:
How to save JAVA Exception printStackTrace on String
Probably you've used sometines the Exception object in try/catch block to print the exception stack trace that help us to find where the line of code where the exception is thrown, but more than once i wanted to get that stack trace in some String variable for example, maybe to save in some auditory table or something like that. I read a little bit about the method getStackTrace but really it doesn't give me the full stack trace.
Well, to do that, first you have to understand that when you call ex.printStackTrace() you print a characters stream on screen, so the way to get that characters stream is using the overloaded version of printStackTrace(PrintWriter objWriter) that receives a PrintWriter object to transfer that characters stream to the PrintWriter object, then you can use that writer to "write" it's stream on something like a file or a simple String.
Check this sample code:
import java.io.File;
import java.io.IOException;
class MyClass {
public static void main(String[] arguments)
{
try
{
(new MyClass()).readInexistentFile();
}
catch(IOException ex)
{
//Catch the stack trace?
}
}
public void readInexistentFile() throws IOException
{
File myFile = new File("hi6.txt");
FileReader myReader = new FileReader(myFile);
myReader.read();
}
}
...
How to sort JAVA collection elements (part 2)

Continuing with "How to sort JAVA collection elements", i want to share with you another way to sort collections that have it's differences to the first way (using Comparable interface) and enables you to have different sort options, i'm talking about use the Comparator interface.
class Employee implements Comparable<Employee> {
private String completeName;
private int age;
private boolean sex;
private String position;
public Employee(String completeName, int age, boolean sex, String position) {
setCompleteName(completeName);
setAge(age);
setSex(sex);
setPosition(position);
}
@Override
public String toString()
{
return getCompleteName() + ", " + getAge() + " years, " +
(isSex()?"male, ":"female, ") + getPosition();
}
public String getCompleteName() {
return completeName;
}
public void setCompleteName(String completeName) {
this.completeName = completeName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public int compareTo(Employee o) {
return getCompleteName().compareTo(o.getCompleteName());
}
}
Wait... Comparable and Comparator? Yes, you read well. The comparator interface is slightly similar to comparable, because it defines a method called compare() that works like compareTo() of Comparable interface, but it has the difference that you don't need to implement Comparator in the collection's class type. To explain that a little bit well, i'm going to resume the example of the last post. I have my Employee class implementing Comparable interface and it's method compareTo() that orders in ascending order based on completeName field (if you haven't read the first post you can download the sample code >>HERE<<).
Now, additional to it, i wan to have the option of order an Employee list based on age field descending or position ascending. How to do that in JAVA?? Continue reading
How to sort JAVA collection elements

Probably you've worked with List in a moment of your JAVA developer life, and we can say that using a List instead an array is more beautiful because the list grows automatically, you can add elements easily (only objects, but if you want to add primitives use wrapper classes) and so on. But, How to sort the elements of a list?
Suppose that you have a list of employees. Those employees have the next data:
complete name age sex position
So, you have your JAVA bean Employeee, like this: (Continue reading the post)
Automatic raw data output – Struts 2
As you know (mainly web developers), when you want to return some kind of data as a response (like a PDF document, an excel file download, and so on) you have to do something like this:
ServletOutputStream out = servletResponseObj.getOutputStream();
servletResponseObj.setContentType("application/pdf");
out.write(myByteArray);
out.flush();
out.close();
return null;
In synthesis, you have to get some stream like an byte array, instantiate an OutputStream object to write the stream in the response buffer and so on. We ask ourselves "How to do this automatically?", well, in Struts2 now is possible to automatically return raw data without interact with OutputStream , HttpServletResponse objects, as you can see next...







