Thursday, September 5, 2013

weblog; cucumber, hibernate and certificates

Cucumber

Github: cucumber/cucumber-jvm

https://github.com/cucumber/cucumber-jvm http://cukes.info/install-cucumber-jvm.html https://github.com/cucumber/cucumber-jvm/tree/master/examples/java-helloworld

Cucumber-JVM is a pure Java implementation of Cucumber that supports the most popular programming languages for the JVM.

https://github.com/cucumber/cucumber-jvm

Hibernate

How do we count rows using Hibernate?

http://stackoverflow.com/questions/1372317/how-do-we-count-rows-using-hibernate
Integer count = (Integer) session.CreateQuery("select count(*) from Books").UniqueResult();

Where 'Books' is the name off the class - not the table in the database.

hibernate.connection.autocommit

https://forum.hibernate.org/viewtopic.php?f=1&t=944848

It sounds like it's a good idea not to turn on auto-commit.

JPA + Hibernate + autocommit

http://stackoverflow.com/questions/1228086/jpa-hibernate-autocommit

The syntax is slightly different for the version of Hibernate I'm using.

NO!
<property name="hibernate.connection.autocommit" value="false"/>

Yes
<property name="hibernate.connection.autocommit">false</property>

remaining connection slots are reserved for non-replication superuser connections

http://community.webfaction.com/questions/12239/remaining-connection-slots-are-reserved-for-non-replication-superuser-connections http://stackoverflow.com/questions/5108876/kill-a-postgresql-session-connection

Manually locate the hanging Postres processes and kill them.

" extract pids from list of  processes
" after running 'ps -ef|grep postgres'
:%s/  \d\+ \(\d\+\) .\+\n\+/ \1/g

Supposedly, a similar outcome can be achieved with this query to the database:

"Before executing this query, you have to REVOKE the CONNECT privileges to avoid new connections:
REVOKE CONNECT ON DATABASE dbname FROM PUBLIC, username;

"Let us begin...
SELECT 
    pg_terminate_backend(pid) 
FROM 
    pg_stat_activity 
WHERE 
    -- don't kill my own connection!
    pid <> pg_backend_pid()
    -- don't kill the connections to other databases
    AND datname = 'database_name'
    ;

org.hibernate Interface Session

http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/Session.html#method_summary http://stackoverflow.com/questions/4588406/replacing-existing-rows-with-new-ones-causes-duplicate-key-exception

When in doubt, read the documentation. There are several things I learned here.

#evict: even after deleting a record, the identifier is still in the cache. Perhaps I ran into this problem because I hadn't closed the session yet or called #commit. Perhaps #evict was a better decision for performance sake in my particular case setting up my integration test.

#rollback: the Session#beginTransaction -> Transaction#commit block does not automatically issue a #rollback if an exception is thrown. You will need to capture exceptions and manually issue a rollback.

#persist: saves a record to the database; not sure if it is supposed to be used for updating, but I use it to save records to the database and use the identifier that I supply. #save will attempt to generate an id before data is persisted to the database.

#flush: queries are queued and reordered in a given transaction. Insert statements come before delete statements, so even if a delete statement is coded before the insert statement, when #commit is invoked, the insert will still come before the delete. Using flush in the transaction tells Hibernate to execute all queries currently in the queue before handling the remaining statements in the transaction. And yes, a #rollback will rollback code both before the #flush and after as long as it is still between the Session#beginTransaction and Transaction#commit.

A typical transaction should use the following idiom:

 Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }

It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.

http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/Session.html#method_summary

SSL and Certificates

Steps to create a csr using keytool and sign with verisign

http://wls4mscratch.wordpress.com/2010/06/08/steps-to-create-a-csr-and-sign-with-verisign/ http://mihail.stoynov.com/2009/03/12/certificates-keystores-java-keytool-utility-and-openssl/
keytool -genkey -alias server -keyalg RSA -sigalg SHA1withRSA -keysize 4096 -keystore server.jks -dname "CN=server, OU=Some Organization, O=Some Office, L=Some City, ST=CA, C=US" -validity 1095 -storepass $STORE_PASS

keytool -certreq -alias server -keystore server.jks -storepass $STORE_PASS > server.csr

keytool - Key and Certificate Management Tool

http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/keytool.html

Comprehensive documentation on how to use keytool.

[K]eytool is a key and certificate management utility. It allows users to administer their own public/private key pairs and associated certificates for use in self-authentication... or data integrity and authentication services, using digital signatures.

No comments:

Post a Comment