Monday, August 27, 2018

Using the PlatformTransactionManager
You can also use the org.springframework.transaction.PlatformTransactionManager
directly to manage your transaction. Simply pass the implementation of the PlatformTransactionManager you are using to your bean through a bean reference. Then, using
the TransactionDefinition and TransactionStatus objects you can initiate transactions, roll
back, and commit.
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
// explicitly setting the transaction name is something that can only be done
programmatically
def.setName("SomeTxName");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
try {
// execute your business logic here
}
catch (MyException ex) {
txManager.rollback(status);
throw ex;
}
txManager.commit(status);

Programatic Transaction Management thru Transaction Template:

public class SimpleService implements Service {
// single TransactionTemplate shared amongst all methods in this instance
private final TransactionTemplate transactionTemplate;
// use constructor-injection to supply the PlatformTransactionManager
public SimpleService(PlatformTransactionManager transactionManager) {
Assert.notNull(transactionManager, "The transactionManager argument must not be
null.");
this.transactionTemplate = new TransactionTemplate(transactionManager);
}
public Object someServiceMethod() {
return transactionTemplate.execute(new TransactionCallback() {
// the code in this method executes in a transactional context
public Object doInTransaction(TransactionStatus status) {
updateOperation1();
return resultOfUpdateOperation2();
}
});
}

}
--------------------------------------------------------------------------------------------------------------------------
Declarative and Programatic Transaction Management in Spring:

Declarative :-  @Transactional Annotation
// these settings have precedence for this method
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void updateFoo(Foo foo) {
        // do something
    }
}

@Transactional settings

The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction". The default @Transactional settings are as follows:
  • Propagation setting is PROPAGATION_REQUIRED.
  • Isolation level is ISOLATION_DEFAULT.
  • Transaction is read/write.
  • Transaction timeout defaults to the default timeout of the underlying transaction system, or to none if timeouts are not supported.
  • Any RuntimeException triggers rollback, and any checked Exception does not.

Monday, August 13, 2018

Proxy Pattern(Structural Pattern):
Allows for object level access control by acting as a pass through entity or a placeholder object. 

Sunday, August 12, 2018

Hibernate OpenSession and getCurrentSession:
 If you set hibernate.current_session_context_class to thread  then you can access that session anywhere else by using the SessionFactory.getCurrentSession().
SessionFactory.openSession() always opens a new session that you have to close once you are done with the operations. SessionFactory.getCurrentSession() returns a session bound to a context - you don't need to close this.

Thursday, August 9, 2018

Spring:Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are
called as follows:
• Methods annotated with @PostConstruct
• afterPropertiesSet() as defined by the InitializingBean callback interface
• A custom configured init() method
Destroy methods are called in the same order:
• Methods annotated with @PreDestroy
• destroy() as defined by the DisposableBean callback interface
• A custom configured destroy() method

Wednesday, August 8, 2018

Uploading a file of any type and size:
----------------------------------------------
bos = new BufferedOutputStream(new FileOutputStream(new File(
file.getAbsolutePath() + File.separator
+ event.getFile().getFileName())));

bis = new BufferedInputStream(event.getFile().getInputstream());
byte[] b = new byte[bis.available()];

while (bis.read(b) != -1)
bos.write(b);
------------------------------------------------
File file = new File("test.txt");
   FileReader fileReader = new FileReader(file);
   BufferedReader bufferedReader = new BufferedReader(fileReader);
   StringBuffer stringBuffer = new StringBuffer();
   String line;
   while ((line = bufferedReader.readLine()) != null) {
    stringBuffer.append(line);
    stringBuffer.append("\n");
Spring Configuration:
------------------------------------
context:annotation-config @Required@Autowired@PostConstruct, and so on.
mvc:annotation-driven @RequestMapping@Controller + @Request/ResponseBody
tx:annotation-driven/> default name is transactionManager
Hibernate3: AnnotationSessionFactoryBean 
Hibernate4:LocalSessionFactoryBean
Wand Specific:
---------------