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 validate through annotations: Struts 2

Hi to all. I've studied some stuff about struts 2 those days so i've learn a lot of new things like this post topic. Normally, what do you do when you want to implement a basic validation on Struts 2? The steps about how to do that are:
- Assure that you're using defaultStack interceptor stack because inside it are workflow and validation interceptors that are responsible of execute validation logic and redirect to appropiate result when validation triggers.
- Define a result named "input" in action's declaration on struts.xml . This is the result where validation must redirect automatically if exists validation errors. If input result is not defined it will give an error of result not found.
- Override validate() method on your action.
- Write your own validation logic inside it, that must add error messages when it fails to properly validation mechanism. For example, if i validate that a field must be a number and greater than 1000, if it doesn't pass the validation it must add a error message using addActionError() or addFieldError() (To follow standards you should use addFieldError() ), like this:
if (fieldOne == null || fieldOne.equals("")) { addFieldError("fieldOne", "Field one must not be empty."); } if (fieldTwo == null || Integer.valueOf(fieldTwo) < 30) { addFieldError("fieldTwo", "Field two must be greater or equal than 30."); } - (Optional) in your jsp you can define <s:fielderror> or <s:actionerror> tags to print your validation messages. As you can guess, <s:fielderror> prints validation errors added with addFieldError() and <s:actionerror> to print action error messages added with addActionError(). To print validation errors from fieldOne and fieldTwo you have to define <s:fielderror> like this:
<s:fielderror cssClass="cssClassForUllistwithLi" > <s:param>fieldOne</s:param> <s:param>fieldTwo</s:param> </s:fielderror>
- Run your app
This is the basic way. But if you want to validate through annotations is easier and faster, you have to do this:
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?
JAVA Singleton Pattern

Hello again, after 3 weeks of posts absense, i'm back
. Back with JAVA design patterns stuff, this time about How to apply Singleton Pattern, basically a pattern that allows you to restrict to instantiate more than one instance of a class in your application. The requirements that you satisfy with Singleton Pattern are:
- Only one instance of a class per application
- If you try to instantiate a second object from singleton class, you'll get the original instance
I decided to write about it because i had a requirement in a project where i'm working right now that involves to handle an object to mantain the user state and other data in a single object, without possibility of instantiate another one and access easily to current object, so i decided to implement the pattern. First, you need to restrict the constructor of the class to avoid to instantiate object in the usual way so we have to declare the constructor as private. next you have to declare a method that, if an object isn't created yet, instantiate a new object and returns it, or, if object is already created, return a reference to that object...
SCJP 6: Done!!

Hi to all, and sorry for post absence. Those last weeks i've been working in some projects and studying for SCJP exam that i re-scheduled to past saturday. Fortunately, i've passed the exam with 85%
woohoo . God helped me with the exam giving me the intelligence to remember JAVA programming rules about OOP, Threads, serialization, inner classes, and soo on. So i feel very well, taking easy those days (is something like a "vacation") and thinking about next certification. I have a dilemma: I don't know if study for:
- SCBCD - Sun Certified Business Component Developer (EJB) or
- SCDJWS - Sun Certified Developer for Java Web Services (web services)
Because i don't know anything or a little bit (in web services) but both technologies are very interesting. Actually i been working with RESTful web services and i like to complement that with EJB 3 but unfortunately current SCBCD is about EJB 2 so i haven't take my desicion yet. But doesn't matter, i wanna rest some days (5 maximum XD) and after that start to sdudying something.
Also i can say that Big Moose Salon Forums helped me a lot. In that forum i found a page with links to mock exams that are very useful to practice after study some topic, and the forum to search my doubts or publish them to receive an answer from most experimented forum members.
Also i going to share some key topics of SCJP in future posts, so keep reading us!!!
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





