Showing posts with label alternatives. Show all posts
Showing posts with label alternatives. Show all posts
Monday, April 24, 2017
Alternatives of Open Session In View OSIV
Alternatives of Open Session In View OSIV
Today I am going to tell you other alternative you can do in order to avoid LazyInitializationException: Session has been closed. If you are using SPRING with HIBERNATE that you can take the advantages of IOC and AOP. These are some functions that you must have to know in order to understand the working of this concept.
session.connect() : It is a function that will be called on a Session object it is responsible for establishing the connection with the database.
session.disconnect() : It will release the connection resources. It doesnt mean you are closing the connection.
session.flush() : this function is used to serialize the data to the database. If you have something in the session that is not submitted to DB you can submit them using session.flush().
So here is the utility class that you can take :
HibernateUtil.java
I am taking one interceptor from where we can call the functions from out utility class and you can call it by using AOP.
here is the code that you can apply on your service layer using AOP.
ConnectionService.java
Now you have to configure this in spring configuration file and you have done.
LazyInitializationException: Session has been closed
session.connect() : It is a function that will be called on a Session object it is responsible for establishing the connection with the database.
session.disconnect() : It will release the connection resources. It doesnt mean you are closing the connection.
session.flush() : this function is used to serialize the data to the database. If you have something in the session that is not submitted to DB you can submit them using session.flush().
So here is the utility class that you can take :
HibernateUtil.java
You can easily understand the HibernateUtil class we have use the Thread Local design pattern that you can see her.
public class HibernateUtil {
@Autowired
private static SessionFactory sessionFactory;
private static final ThreadLocalthreadsession=new ThreadLocal ();
private static final ThreadLocalthreadtransaction=new ThreadLocal ();
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void connect() {
try {
Session session=threadsession.get();
if(session==null) {
getSession();
session=threadsession.get();
}
session.reconnect();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void diconnect() {
Session session=threadsession.get();
session.disconnect();
}
public Session getSession() {
Session session=threadsession.get();
try {
if(session==null) {
session=sessionFactory.openSession();
threadsession.set(session);
}
}
catch(Exception e) {
e.printStackTrace();
}
return session;
}
public void closeSession() {
try {
Session session=threadsession.get();
threadsession.set(null);
if(session!=null && session.isOpen())
session.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void beginTransaction() {
Transaction tx=threadtransaction.get();
try {
if(tx==null) {
tx=getSession().beginTransaction();
threadtransaction.set(tx);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public void commitTransaction() {
Transaction tx=threadtransaction.get();
try {
if(tx!=null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
threadtransaction.set(null);
}
catch(Exception e) {
rollbackTransaction();
e.printStackTrace();
}
}
public void rollbackTransaction() {
Transaction tx=threadtransaction.get();
try {
threadtransaction.set(null);
if(tx!=null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
closeSession();
}
}
}
I am taking one interceptor from where we can call the functions from out utility class and you can call it by using AOP.
here is the code that you can apply on your service layer using AOP.
ConnectionService.java
public class ConnectionService {
@Autowired
HibernateUtil2 hutil;
public void startConnection() {
hutil.connect();
hutil.beginTransaction();
System.out.println("before");
}
public void closeConnection() {
System.out.println("after");
hutil.commitTransaction();
hutil.diconnect();
hutil.getSession().flush();
}
}
Now you have to configure this in spring configuration file and you have done.
LazyInitializationException: Session has been closed
Available link for download
Friday, March 31, 2017
Alternatives to Real Pumpkins
Alternatives to Real Pumpkins
Sadly, not everyone can have a real pumpkin for the Halloween season, carved or otherwise. Some places simply dont have them for sale, or theyre marked up at ridiculous prices. Still other people might not be allowed by their landlords to have one, or dont want to deal with disposal at the end of the season. So, what other options do you have if you want pumpkin decor for Halloween and the autumn without all the fuss of a real pumpkin? Here are some ideas:

Plastic Pumpkins
Fake foam or plastic pumpkins are abound in craft stores and now is a good time to pick one up. They come in orange, white, black, and chalkboard at Michaels. You can leave it plain or give it a makeover with dripped crayons (rainbow on a white pumpkin is great, but black/silver/gray or Halloween colors would be great too), adding craft ribbons or bows, adding a glow-in-the-dark treatment, covering it in fake gems, or painting it to match your decor.

Source.
Velvet PumpkinsI re-pinned this idea onto my Halloweentown Ideas Pinterest board a few months back and I still absolutely adore it. A stuffed pumpkin made out of a rich, fall-like fabric is a great option and a good way to spend a rainy afternoon. Heres a tutorial from HGTV for easy sewn pumpkins which can be done in velvet, burlap, cotton, or any other fabric you can think of.

Source.
Origami
Making an origami pumpkin is actually super simple and you can do it with any kind of paper. Patterned scrapbook paper is a wonderful way to add a Gothy touch or to match your own home decor. For a simple tutorial, check here on Origami Instructions.

Source.
Mercury Glass
Mercury glass is a very pretty material that works especially well for Halloween decor. Something about the reflective but distorted surface is a really refreshing take on the traditional orange pumpkin and great if youre not crazy about incorporating tons of orange into your Halloween look. This one above is from Pottery Barn, or you can look here for a tutorial on adding a Mercury-Glass look to any glass.

Carved Turnips
If you want an alternative to a large pumpkin but you want something organic still, consider buying a turnip to carve! Turnips are more traditional in certain places and are much more space efficient. For a turnip carving tutorial, check here on Makezine!
Do you prefer real pumpkins for your Halloween decor, or do you add in fake ones as well?
Available link for download
Labels:
alternatives,
pumpkins,
real,
to
Subscribe to:
Posts (Atom)