05 August 2017
It's been a while since my last blog post, but Today I'd like to talk about GR8Conf US 2017.
This was my second time attending GR8Conf US and it was very special because I had the opportunity to give my first talk in a conference! Before going into the details of the 2017 edition, I want to share a little bit more of why this is very important for me.
When I started to work with Groovy/Grails in 2010 I was totally new to software development using JVM related techonologies and I found the Groovy community a very friendly one. A year later I started enjoy Grails so much that I became some sort of advocate for it and started to follow people working heavily on Grails like Graeme Rocher, Jeff Brown, Ted Naleid, Burt Beckwith, Bobby Warner, Ryan Vanderwerf, Dhiraj Dmahapatro, Colin Harrington and others.
Few months/years later I started to see Minneapolis as a great place to work with Groovy, companies like Target, Object Partners and Refactr had a lot of employees contributing to the community on a daily basis. I wanted to travel to Minneapolis for GR8Conf and meet all these cool people, unfortunately, I didn't have the money to pay for my travel expenses.
I finally decided that I wanted a job in Minneapolis but I couldn't land one since my show stopper was the need for a work Visa. During that time I met great people that gave me advices for my career and that helped me to land a job in Los Angeles, California... I'd like to thanks Craig Burke and Dan Woods for answering personal questions during that time.
A few more years later, I finally had the opportunity to attend GR8Conf US last year and had a wonderful time! After that I knew I wanted to live in Minneapolis and one day speak at the conference, a couple of months later I signed a job offer with Target and here I am... enjoying everyday, working in cool things with very talented people.
So one of my career goals was always to speak at GR8Conf, I can check it as done!
Going back to GR8Conf, I'd like to share some interesting talks that I attended to, hopefully the videos will be online soon!
In the morning I went to the “Amazon Alexa Workshop” workshop by Ryan Vanderwerf and Lee Fox. This session covered all the fundamentals to write AWS Lambad with Groovy and create your first Alexa skill. It was very fun to use Groovy for something like this, if you are looking for things to do on your spare time check it out!
In the afternoon I attended Daniel Hyun's “Hands on Ratpack” workshop. I use Ratpack everyday to write high performant APIs but I really wanted to attend Danny's workshop because I knew it was TDD! It was fun but Danny, I'm sorry, I cheated and did it in Groovy :(
Graeme Rocher kicked off the first day of talks with a keynote detailing the current state of Grails and the future, which is very exciting! Although I don't use Grails on a daily basis, it was great to see how Grails is evolving so quickly and adding very cool features on Grails 3.3 and upcoming releases. The engineering behind Grails is something that always amazes me, very cool stuff
During the first day I attended to the following talks:
After that we headed to the Object Partners Meet & Greet where I had the opportunity to talk with very nice guys doing some cool stuff. It was great to catch up with people that I already knew and made new friends.
Of course we had the Groovy Puzzles hosted by Baruch (@jbaruch) and his especial guest Daniel Woods (@danveloper). As you can imagine they had great groovy puzzles and drinking rules. At the end, Dan and Baruch finished a bottle of bulleit rye drinking shots. Dan also gave a few copies of this book "Learning Ratpack", if you don't have this book I encourage you to buy it. Not only because of the great quality of the book, but also because all the royalties will be donated to the GR8Ladies.
The second day was the best:
To close this awesome conference, Paul King gave the talk "Groovy Update: What's new in Groovy 2.5+". He talked about new upcoming features in Groovy, very exciting stuff... really looking forward to the parrot.
After that we headed to The Local to have some appetizers and drinks. I really enjoyed this networking event.
During the conference I had the opportunity and time to talk about software and everyday life with very talented and interesting people: Dhiraj Mahapatro (@dhirajmahapatro), Eric Helgeson (@nulleric), Bobby Warner (@bobbywarner), Søren Berg Glasius (@sbglasius), Daniel Woods (@danveloper), Doug Sabers (@lightsabersd), Evan Murphy, Izaak Lauer, Ivan Lopez (@ilopmar), Ken (@kktec), Burt Beckwith (@burtbeckwith), Steve Pember (@svpember), Daniel Hyun (@Lspacewalker), Ryan Vanderwerf (@RyanVanderwerf), Aaron Zirbes (@Zirbes), Shaun Jurgemeyer (@sjurgemeyer) Ted Naleid (@tednaleid) and so many others (btw you should follow them on Twitter)
I would like to thank Doug Sabers and the GR8Conf US organizers for such a wonderful job! Overall GR8Conf US 2017 was a fantastic conference, and I’m already looking forward to attending Gr8Conf 2018!
I had a great time. I think everyone knows that the Groovy community is full of talented people, they are great human beings, kind and friendly... Software is a passion that drives everyone but there is more than that and I'm looking forward to contribute back to it and see them again soon.
16 September 2016
H2 is a relational database management system written in Java. It can be embedded in Java applications or run in the client-server mode
When you use Grails for a new project, the default DBMS (DataBase Management System) is H2. A cool characteristic of this DBMS is its capability to handle in-memory tables: it is possible to create a database living in the RAM memory.
In Grails 3.x your database configuration might look as follows:
dataSource:
pooled: true
jmxExport: true
driverClassName: org.h2.Driver
username: sa
password:
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
One nice feature of IntelliJ is the Database Tool Window.
Wouldn't be nice to connect to your H2 in memory database using IntelliJ? Well that's possible using a TCP Server for client/server connections.
You can accomplish this by simply doing the following in your BootStrap.groovy file
import org.h2.tools.Server
class BootStrap {
final String[] args = [
"-tcpPort", "8092",
"-tcpAllowOthers"]
Server server
def init = { servletContext ->
server = Server.createTcpServer(args).start()
}
def destroy = {
server.stop()
}
}
Doing this will start a TCP server on port 8092 that allow others to connect
IMPORTANT: This is an example for development/test environment, you should modify this behavior for production environment.
After we start our app, we can use the Database Tool Window and add a connection to a remote H2 database
I hope you find this helpful.
01 August 2016
This was my first time attending GR8Conf and had a wonderful time and definitely it was a great experience. As I mentioned to a few guys, I always wanted to attend but the circumstances were not appropriate on previous years but finally I did it!
In the morning I went to the “Getting Started With Grails 3” workshop by Jeff Scott Brown. This session covered all the fundamentals of the framework, probably better as an introduction to developers not familiar with the framework but Jeff also included tips and tricks for developers with experience.
In the afternoon I attended Jeff Brown’s “REST With Grails 3” workshop. He explained Grails capabilities to build RESTful services with a small amount of code, explained the content negotation and more. The best part was his introduction to JSON views (more to come).
Graeme Rocher kicked off the first day of talks with a keynote detailing the current state of Grails and the future, which is very exciting! He talked about all the new cool features in Grails 3.2: GORM 6 Suite which includes RxGORM, AngularJS 1.0 Scaffolding and JSON Views 1.1. I can't describe how excited I was when I saw all these cool features, Grails is a solid framework and evolving very fast thanks to the hard work of the OCI team and the community.
During the first day I attended to the following talks:
By the end of the day, Ken Kousen talk about the experiences as a Groovy Developer in the Java World. Believe me, this talk was so energizing, thanks Ken for remember us how awesome are the groovy based technologies and this community. Oh yes, don't forget to use Kotlin (just kidding)
After that we headed to the Object Partners Meet & Greet where I had the opportunity to talk with very nice guys doing some cool stuff. It's funny because I live in Los Angeles and I never had the chance to meet Joshua Tuberville from VirtualTourist, cool guy.
Of course we had the Groovy Puzzles hosted by Baruch (@jbaruch) and his especial guest Daniel Woods (@danveloper). As you can imagine they had great groovy puzzles and drinking rules. At the end, Daniel gave away two copies of his book "Learning Ratpack". If you don't have this book I encourage you to buy it. Not only because of the great quality of the book, but also because all the royalties will be donated to the GR8Ladies.
The second day was great too:
To close this awesome conference, John Engelman (@johnrengelman) gave the talk "Writing Software in the Age of Agile: Microservices, Cloud, Containers, & DevOps". He talked about new technologies and ideas that help to refine that it means to develop software in an agile environment and making a lot of emphasis in doing things faster with high quality. It was a great talk, specially if you care about the whole SDLC and the quality of your work on a daily basis.
As a special recording, Ken Kousen and Baruch recorded the Groovy Podcast live from GR8Conf. If you haven't watched it do it now! https://www.youtube.com/watch?v=MWRWnKfbnIs
After that we headed to Gr8Ladies & Gr8Allies Networking Meetup to have some appetizers and drinks. I really enjoyed this networking event.
During the conference I had the opportunity and time to talk about software and everyday life with very talented and interesting people: Craig Burke (@craigburke1), Søren Berg Glasius (@sbglasius), Daniel Woods (@danveloper), Jenn Strater (@codeJENNerator), Puneet Behl (@puneetbhl), Ken (@kktec), Ken Kousen (@kenkousen), Burt Beckwith (@burtbeckwith), Baruch Sadogursky (@jbaruch), Steve Pember (@svpember), Danny Hyun (@Lspacewalker), Marcin Erdmann (@marcinerdmann), Ryan Vanderwerf (@RyanVanderwerf), Kyle Boon (@kyleboon), John Engelman (@johnrengelman), Jeff Warren (@jekewa), Joshua Tuberville (@jtuberville), Aaron Zirbes (@Zirbes), Shaun Jurgemeyer (@sjurgemeyer) and so many others (btw you should follow them on Twitter)
I would like to thank the GR8Conf US organizers for such a wonderful job! Overall GR8Conf US 2016 was a fantastic conference, and I’m already looking forward to attending Gr8Conf 2017!
Also, special thanks to Jenn Strater (@codeJENNerator) for all your help and support before and during the conference, I really appreciate it! Congrats again on your next adventure in Denmark and looking forward to meet again.
I had a great time. I think everyone knows that the Groovy community is full of talented people, they are great human beings, kind and friendly... Software is a passion that drives everyone but there is more than that and I'm looking forward to contribute back to it and see them again soon.
17 January 2016
As we know, Grails services by default have automatic transaction management such as propagation and so on. By setting transactional to false you disable automatic Spring managed transaction demarcation for every method in the service. When is set to the true, The propagation level of the transaction is by default PROPAGATION_REQUIRED. Also we can use the @ Transactional and @ NotTransactional annotations.
What is a transaction? In simple terms, a transaction is:
But, what is happening in our database when we use transactions? The purpose of this post is to present some small code examples and the behavior at database level. There are some requirements for them to run the following examples:
First, we can start creating a Domain Class and a Service to examine how a transaction works:
class Person {
String name
}
@Transactional
class PersonService {
def save() {
new Person(name: "Roberto").save()
new Person(name: "Raj").save()
}
}
Now, if we invoke the save method, we can see the following in our database log.
140605 8:42:42 11 Query SET autocommit=0
11 Query insert into person (version, name) values (0, 'Roberto')
11 Query insert into person (version, name) values (0, 'Raj')
11 Query commit
11 Query SET autocommit=1
As you can see, is setting the autocommit to 0, inserting, commit, set autocommit 1. This is what we are expecting to do, this is the way we handle transactions.
Now let's see what happens when we add another method:
@Transactional
class PersonService {
def save() {
new Person(name: "Roberto").save()
new Person(name: "Raj").save()
anotherSave()
}
def anotherSave() {
new Person(name: "anotherPerson").save()
}
}
If we invoke the save method again we will get this result:
140605 8:48:58 11 Query SET autocommit=0
11 Query insert into person (version, name) values (0, 'Roberto')
11 Query insert into person (version, name) values (0, 'Raj')
11 Query insert into person (version, name) values (0, 'anotherPerson')
11 Query commit
11 Query SET autocommit=1
By default, the Grails transactions have REQUIRED propagation, and in this case the 'anotherSave' method will join the current transaction.
But what if we need to run the 'anotherMethod' in another transaction? Well, we can just change the transaction's propagation to a REQUIRES_NEW. This will look like this:
@Transactional
class PersonService {
def save() {
new Person(name: "Roberto").save()
new Person(name: "Raj").save()
anotherSave()
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
def anotherSave() {
new Person(name: "anotherPerson").save()
}
}
Again, we can invoke the save method and see the results:
140605 8:58:41 11 Query SET autocommit=0
11 Query insert into person (version, name) values (0, 'Roberto')
11 Query insert into person (version, name) values (0, 'Raj')
10 Query SET autocommit=0
10 Query insert into person (version, name) values (0, 'anotherPerson')
10 Query commit
10 Query SET autocommit=1
11 Query commit
11 Query SET autocommit=1
At first glance we can see the change in autocommit but the most important thing are the numbers (10 and 11). These numbers represent the thread id, with this we know that they are running in another transaction.
Now, let's see what happens when a RuntimeException occurs in save method after invoking anotherSave. If you want, truncate our table after that, can change our code to this:
@Transactional
class PersonService {
def save() {
new Person(name: "Roberto").save()
new Person(name: "Raj").save()
anotherSave()
throw new RuntimeException("oh!")
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
def anotherSave() {
new Person(name: "anotherPerson").save()
}
}
If we invoke the save method we will get the following in our database log:
140605 9:06:19 11 Query SET autocommit=0
11 Query insert into person (version, name) values (0, 'Roberto')
11 Query insert into person (version, name) values (0, 'Raj')
10 Query SET autocommit=0
10 Query insert into person (version, name) values (0, 'anotherPerson')
10 Query commit
10 Query SET autocommit=1
11 Query rollback
11 Query SET autocommit=1
Ok, we know we have two transactions, let's make query to the database.
mysql> select * from person;
+----+---------+---------------+
| id | version | name |
+----+---------+---------------+
| 3 | 0 | anotherPerson |
+----+---------+---------------+
1 row in set (0.01 sec)
mysql>
As you can see, we have a new record in our database, this was generated by the method anotherSave. This is because we are running this statement in another transaction.
IMPORTANT: automatic rollback occurs if a method throws a runtime exception (i.e. one that extends RuntimeException) or an Error. Checked exceptions do not roll back transactions.
Ok, by now you know that the transaction disables autocommit to execute one or more statements and then do the commit.
Now imagine a scenario in which you require that the statements of anotherSave should be with auto-commit (MySQL has autocommit enabled by default). Let's take a look on this... first, we will put a few examples of possible ideas that won't work:
FAIL: Using @NotTransactional annotation
@Transactional
class PersonService {
def save() {
new Person(name: "Roberto").save()
anotherSave()
new Person(name: "Raj").save()
}
@NotTransactional
def anotherSave() {
new Person(name: "anotherPerson").save()
}
}
With this approach our database log will be:
140605 9:16:34 11 Query SET autocommit=0
11 Query insert into person (version, name) values (0, 'Roberto')
11 Query insert into person (version, name) values (0, 'anotherPerson')
11 Query insert into person (version, name) values (0, 'Raj')
11 Query commit
11 Query SET autocommit=1
Look at this... is under the same thread. So this option is discarded.
FAIL: Using Groovy SQL without change the propagation
Well... maybe we can think something like "If MySQL sets autocommit by default we can use SQL with GroovySQL and done". The service can be like this:
@Transactional
class PersonService {
def save() {
new Person(name: "Roberto").save()
anotherSave()
new Person(name: "Raj").save()
}
def sessionFactory
def anotherSave() {
def sql = new Sql(sessionFactory.currentSession.connection())
sql.execute("INSERT INTO `test`.`person` (`version`, `name`) VALUES ('0', 'anotherPerson')")
}
}
Ok... run this and you will see all in one transaction:
140605 9:21:15 21 Query SET autocommit=0
21 Query insert into person (version, name) values (0, 'Roberto')
21 Query INSERT INTO `test`.`person` (`version`, `name`) VALUES ('0', 'anotherPerson')
21 Query insert into person (version, name) values (0, 'Raj')
21 Query commit
21 Query SET autocommit=1
So this is not working as we expected... oh but maybe "we can just change the connection to autocommit".
FAIL: Using Groovy SQL and change the connection to autocommit
@Transactional
class PersonService {
def save() {
new Person(name: "Roberto").save()
anotherSave()
new Person(name: "Raj").save()
}
def sessionFactory
def anotherSave() {
def sql = new Sql(sessionFactory.currentSession.connection())
sql.connection.setAutoCommit(true)
sql.execute("INSERT INTO `test`.`person` (`version`, `name`) VALUES ('0', 'anotherPerson')")
sql.connection.setAutoCommit(false)
}
}
Execute this and look:
140605 9:22:47 21 Query SET autocommit=0
21 Query insert into person (version, name) values (0, 'Roberto')
21 Query SET autocommit=1
21 Query INSERT INTO `test`.`person` (`version`, `name`) VALUES ('0', 'anotherPerson')
21 Query SET autocommit=0
21 Query insert into person (version, name) values (0, 'Raj')
21 Query commit
21 Query SET autocommit=1
Oh! is changing the autocommit but wait... is the same thread. What is happening with the first query. Of course this seems to be ok because we are inserting everything and the code is not throwing a RuntimeException or Error. Lets add a RuntimeException and run it again. For this, we will truncate our table.
@Transactional
class PersonService {
def save() {
new Person(name: "Roberto").save()
anotherSave()
new Person(name: "Raj").save()
throw new RuntimeException("Oh!")
}
def sessionFactory
def anotherSave() {
def sql = new Sql(sessionFactory.currentSession.connection())
sql.connection.setAutoCommit(true)
sql.execute("INSERT INTO `test`.`person` (`version`, `name`) VALUES ('0', 'anotherPerson')")
sql.connection.setAutoCommit(false)
}
}
Execute this and you will see the same query log but look at your table data:
mysql> select * from person;
+----+---------+---------------+
| id | version | name |
+----+---------+---------------+
| 1 | 0 | Roberto |
| 2 | 0 | anotherPerson |
+----+---------+---------------+
2 rows in set (0.00 sec)
mysql>
Yes we set the autocommit to true but we were under the same transaction so our first query was commited too and we don't want this.
This is where we can use another propagation. Let's try it with NOTSUPPORTED and use GORM again. NOTSUPPORTED execute non-transactionally, suspend the current transaction if one exists.
OK: Use NOT_SUPPORTED propagation
@Transactional
class PersonService {
def save() {
new Person(name: "Roberto").save()
anotherSave()
new Person(name: "Raj").save()
throw new RuntimeException("Oh!")
}
@Transactional(propagation = Propagation.NOT_SUPPORTED)
def anotherSave() {
new Person(name: "another Person").save()
}
}
Let's try this piece of code. We will truncate our table before that.
140605 9:29:14 21 Query SET autocommit=0
21 Query insert into person (version, name) values (0, 'Roberto')
20 Query insert into person (version, name) values (0, 'another Person')
21 Query insert into person (version, name) values (0, 'Raj')
21 Query rollback
21 Query SET autocommit=1
Take a look on the thread ids, our another person statement is running in another thread and using the autocommit behaviour from MySQL. Now, look at our data
mysql> select * from person;
+----+---------+----------------+
| id | version | name |
+----+---------+----------------+
| 2 | 0 | another Person |
+----+---------+----------------+
1 row in set (0.00 sec)
mysql>
We only have the record that we want and because we had an exception, the transaction in the save method became a rollback. This is a nice approach.
IMPORTANT: We know that maybe is not common to go under an auto-commit rule but you never know maybe tomorrow you will need it
To finish this small post, here is a list of the available propagations:
we deal with important data to the business, so we must pay close attention to avoid any data loss problem while running transactions
These examples were developed along with Raj Govindarajan.
Hope this can help you in the future.
19 April 2015
A few months ago I was asked to create a login with ReCaptcha using spring security core and recaptcha plugins. At that time I made a plugin for solving the requirement Spring Security Plugin Recaptcha. Today I want to share another idea to accomplish this.
Assuming the application has ReCaptcha and spring security core the first thing to do is add the ReCaptcha widget our loginview, usually auth.gsp.
<p>
<label for='username'><g:message code="springSecurity.login.username.label"/>:</label>
<input type='text' class='text_' name='j_username' id='username'/>
</p>
<p>
<label for='password'><g:message code="springSecurity.login.password.label"/>:</label>
<input type='password' class='text_' name='j_password' id='password'/>
</p>
<p>
<recaptcha:ifEnabled>
<recaptcha:recaptcha theme="clean"/>
<recaptcha:ifFailed>CAPTCHA Failed</recaptcha:ifFailed>
</recaptcha:ifEnabled>
</p>
Then generate a filter (AuthFilter.groovy) to override the 'authenticationProcessingFilter' bean (this filter subclasses the plugin's RequestHolderAuthenticationFilter to Maintain its functionality). You can name this filter as you like and put it under /src/groovy folder.
import com.megatome.grails.RecaptchaService
import grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter
import org.springframework.security.authentication.BadCredentialsException
import org.springframework.security.core.Authentication
import org.springframework.security.core.AuthenticationException
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler
import org.springframework.web.context.request.RequestContextHolder
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
class AuthFilter extends RequestHolderAuthenticationFilter {
private String recaptcha_response
private String recaptcha_challenge
private String remoteAddr
private String failureUrl
RecaptchaService recaptchaService
private SimpleUrlAuthenticationFailureHandler failureHandler = new SimpleUrlAuthenticationFailureHandler()
@Override
Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
def session = RequestContextHolder.currentRequestAttributes().getSession()
recaptcha_response = request.getParameter("recaptcha_response_field")
recaptcha_challenge = request.getParameter("recaptcha_challenge_field")
remoteAddr = request.getRemoteAddr()
Map params = ["recaptcha_response_field": recaptcha_response, "recaptcha_challenge_field": recaptcha_challenge]
// Check if valid
if (!recaptchaService.verifyAnswer(session, remoteAddr, params)) {
// Redirect user to login page
failureHandler.setDefaultFailureUrl(failureUrl)
failureHandler.onAuthenticationFailure(request, response, new BadCredentialsException("Captcha invalid!"))
return
}
super.attemptAuthentication(request, response)
}
public void setFailureUrl(String failureUrl) {
this.failureUrl = failureUrl
}
}
Finally, we will register this bean in our resources.groovy as follows:
beans = {
authenticationProcessingFilter (AuthFilter) {
def conf = SpringSecurityUtils.securityConfig
authenticationManager = ref('authenticationManager')
failureUrl = conf.failureHandler. defaultFailureUrl
recaptchaService = ref('recaptchaService')
storeLastUsername = conf.apf.storeLastUsername
}
}
Once done, we can use our ReCaptcha widget as part of our authentication process.
I hope this has been helpful.
Older posts are available in the archive.