Let us share our views in java. Not only java, we can also share examples and configurations on Spring, Hibernate, javascript, CSS and Sqlserver queries. This blog will help you in giving the complete information and resolving your complete java related problems like the problems in core java, servlets, jsp spring, hibernate. And also the web technologies which includes all css related problems(Web 2.0).and java script and the latest smart jquery java script frame work
Jul 29, 2009
How DB locking system works in Hibernate (In Concurrency operations)
There are two common strategies when dealing with updates to database
records, pessimistic locking and optimistic locking.
"Optimistic locking" is more scalable than pessimistic locking when dealing with a
highly concurrent environment. However pessimistic locking is a better solution for situations
where the possibility of simultaneous updates to the same data by multiple sources
(for example, different applications use same database to update) is common, hence making the possibility of "data clobbering" , a
likely scenario. Lets look at a brief explanation of each of these two locking strategies.
Pessimistic locking is when you want to reserve a record for exclusive update by
locking the database record(entire table). Hibernate supports pessimistic locking
(using the underlying database, not in-memory) via one of the following methods:
1. Session.get
2. Session.load
3. Session.lock
4. Session.refresh
5. Query.setLockMode
Although each of the methods accepts different params, the one common parameter
across all is the LockMode class, which provides various locking modes such as NONE,
READ, UPGRADE, UPGRADE_NOWAIT, and WRITE.
For example, to obtain a Timesheet record for updating,
we could use the following code: (assume database supports locking system):
public Timesheet getTimesheetWithLock(int timesheetId)
{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Timesheet timesheet = (Timesheet)session.get(Timesheet.class,
new Integer(timesheetId), LockMode.UPGRADE);
session.getTransaction().commit();
session.close();
return timesheet;
}
Optimistic locking means that you will not lock a given database record or table and
instead check a property of some sort (for example, a timestamp column) to
ensure the data has not changed since you read it. Hibernate supports this using a
version property, which can either be checked manually by the application or automatically
by Hibernate for a given session. For example, the following code excerpt is taken
verbatim out of the Hibernate reference documentation and shows how an application
can manually compare the oldVersion with the current version using a getter method
(for example):
// foo is an instance loaded by a previous Session
session = factory.openSession();
int oldVersion = foo.getVersion();
session.load( foo, foo.getKey() );
if ( oldVersion!=foo.getVersion )
throw new StaleObjectStateException();
foo.setProperty(“bar”);
session.flush();
session.connection().commit();
session.close();
StaleObjectStateException, is an exception in the org.hibernate package.
Jul 27, 2009
Velocity example with FOR LOOP and Date format
import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.DateTool;
public class VelocityExp {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/test/VelocityExp.html");
VelocityContext ctx = new VelocityContext();
Set
Demov d1 = new Demov();
d1.setName("one");
d1.setAge("24");
d1.setKe(new demo2("one"));
Demov d2 = new Demov();
d2.setName("two");
d2.setAge("23");
d2.setKe(new demo2("two"));
Demov d3 = new Demov();
d3.setName("true");
d3.setAge("22");
d3.setKe(new demo2("three"));
Demov d4 = new Demov();
d4.setName("ofour");
d4.setAge("21");
d4.setKe(new demo2("four"));
names.add(d1);
names.add(d2);
names.add(d3);
names.add(d4);
ctx.put("products",names);
ctx.put("date", new DateTool());
ctx.put("dbdate",new Date());
ctx.put("rowCount",new Integer(1));
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
Html file content:
<html>
<head>
<title>velocity test</title>
</head>
<body>
<table>
#foreach($product in $products)
#if ($rowCount % 2 == 0)
#set ($bgcolor = "#0000FF")
Applying BLUE color here
#else
Applying RED color here
#set ($bgcolor = "#FF0000")
#end
<tr>
<td bgcolor="$bgcolor">$product.name</td>
<td bgcolor="$bgcolor">$product.age</td>
<td bgcolor="$bgcolor">$product.ke.k</td>
</tr>
#set ($rowCount = $rowCount + 1)
#end
</table>
Displaying how to use date format here:
$dbdate
$date.get('medium')
$date.format('medium',$dbdate)
</body>
</html>
Jul 25, 2009
Java class with regExp
public class Name {
public static void main(String[] args) {
System.out.println(Name.class.getName().replaceAll(".", "/") + ".class");
}
}
Output:
////////.class
actually everyone thinks the output will be com/Name.class
In the case of replace '.' will treat as regExp, in regExp '.' means any type of character, here replacing with '/' , and it becomes output like "////////.class "
so, for expected answer, change the code as follows,
Name.class.getName().replaceAll("\\.", "/") + ".class");
then the output will, what you exprected,
Output:
com/Name.class
Jul 21, 2009
Small Answer for Small java pzzule!
/* What declaration turns this loop into infinitive loop.*/
public class GhostOfLooper {
public static void main(String[] args) {
// Place your declaration for i here
while (i != 0)
i >>>= 1;
}
}
Solution:
// Place your declaration for i here
byte i = -1;
Solution for java puzzles(3)
public class Looper {
public static void main(String[] args) {
// Place your declaration for i here
while (i == i + 1) {
}
}
}
Solution:
// Place your declaration for i here
double i = 1.0/0.0